1

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?

2
  • 1
    Do you know how the $scope works? Commented Jan 21, 2015 at 13:50
  • Is this thread alive? Commented Feb 23, 2017 at 15:04

1 Answer 1

4
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";
}])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.