简体   繁体   中英

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. 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. You can add as many items to the $scope as you want, an example below:

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";
}])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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