简体   繁体   English

如何从控制器传递数据以在angularjs中查看

[英]How to pass data from controller to view in angularjs

I know how to pass data from view to controller Here is the reference I want to pass data from controller to view in angular-js. 我知道如何从视图到控制器这里传递数据是参考我想从控制器传递数据,以查看在角JS。 Is it possible if so,How it works? 如果可以的话,它如何工作?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM