angularjsHow do I bind data to an AngularJS controller?
In order to bind data to an AngularJS controller, you can use the $scope object. $scope is an object that binds the controller and the view together.
Example
// controller
myApp.controller('myCtrl', function($scope) {
$scope.name = 'John Doe';
});
// view
<div ng-controller="myCtrl">
{{ name }}
</div>
Output example
John Doe
The code above binds the name property of the $scope object to the view. The view will output the value of the name property, which is John Doe.
Code explanation
myApp.controller('myCtrl', function($scope) { ... }- This is the controller, which sets up the$scopeobject.$scope.name = 'John Doe';- This is the binding of thenameproperty of the$scopeobject to the valueJohn Doe.<div ng-controller="myCtrl"> ... </div>- This is the view, which binds the controller to the view using theng-controllerdirective.{{ name }}- This is the expression that will output the value of thenameproperty of the$scopeobject.
Helpful links
More of Angularjs
- How can I use AngularJS to transform XLTS files?
- How do I use AngularJS to watch for changes in a variable?
- How can I use AngularJS to construct an XSS payload?
- How do I use the window.open function with AngularJS?
- How do I create a link in AngularJS?
- How can I add a PDF viewer to my AngularJS application?
- How can I use AngularJS to watch for changes in my data?
- How do I use the AngularJS Wiki to find information about software development?
- How can I use AngularJS UI Router to create an application with multiple views?
- How do I use AngularJS to select an item from a list?
See more codes...