Shailendra Madda
Shailendra Madda

Reputation: 21531

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?

Upvotes: 1

Views: 11530

Answers (1)

jonode
jonode

Reputation: 797

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

Upvotes: 4

Related Questions