[英]How to pass data from controller to view in angularjs
angular.module('yourApp').controller('Contacts', ['$scope',function($scope){
$scope.contacts = [
{name:'Hazem', number:'01091703638'},
{name:'Taha', number:'01095036355'},
{name:'Adora', number:'01009852281'},
{name:'Esmail', number:'0109846328'}
];
}])
In the above code "contacts" will be passed to the view. 在上面的代码中,“联系人”将传递给视图。 It will then be available within the controlled element like so: 然后它将在受控元素中可用,如下所示:
<div ng-controller="Contacts">{{contacts[0].name}}</div>
So we defined the controller we want to use, then we can access the $scope object it passed to the view. 因此,我们定义了要使用的控制器,然后我们可以访问传递给视图的$ scope对象。 You can add as many items to the $scope as you want, an example below: 您可以根据需要向$ scope添加任意数量的项目,以下示例:
angular.module('yourApp').controller('Contacts', ['$scope',function($scope){
$scope.contacts = [
{name:'Hazem', number:'01091703638'},
{name:'Taha', number:'01095036355'},
{name:'Adora', number:'01009852281'},
{name:'Esmail', number:'0109846328'}
];
$scope.rabbit = "Rabbit";
}])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.