Controller of Angularjs

Source: Internet
Author: User

The role of the controller in ANGULARJS is to enhance the view, which is actually a function to add additional functionality to the scope in the view, which we use to set the initial state for the scope object and to add custom behavior.

When we create a controller on the page, Angularjs generates and passes a $scope to the controller, because ANGULARJS automatically instantiates the controller, so we just need to write the constructor. The following example shows the controller initialization:

function my Controller ($scope) {  $scope. msg= "hello,world!" ;  }

The above method of creating a controller pollutes the global namespace, the more reasonable way is to create a module, and then create a controller in the module, as follows:

var myapp=angular.module ("myApp", []); Myapp.controller ("Mycontroller",function($ Scope) {  $scope. msg= "hello,world!" ;})

With the built-in instruction Ng-click, you can bind any other DOM element, such as a button, link, or a click event. Ng-click means
The MouseUp event in the browser is bound together with the event handlers set on the DOM element (for example, when the browser
A click event is triggered on a DOM element and the function is called). Similar to the previous example, the binding looks like this:

<div ng-controller= "Firstcontroller" >

Buttons and links are bound to an operation on an internal $scope, and when you click on any element Angularjs will
Call the appropriate method. Note that when you set which function to call, a parameter is passed in parentheses (add (1)).

function= 0function(amount) {$scope. Counter +=function(amount) { $scope. Counter-= amount;};});

The biggest difference between ANGULARJS and other frameworks is that the controller is not suitable for performing DOM operations, formatting or data manipulation, and state maintenance operations other than the storage data model, which is simply a bridge between the view and the $scope.

Controller nesting (Scope containment scope)

Any part of the ANGULARJS application, regardless of the context in which it is rendered, has a parent scope exists. For
At Ng-app's level, its parent scope is $rootscope.

By default, Angularjs cannot find a property in the current scope, it is checked in the parent scope
Find. If Angularjs cannot find the corresponding attribute, it will search up the parent scope until it arrives $rootscope
So far. If it is not found in $rootscope, the program will continue to run, but the view cannot be updated.

Take a look at this behavior by example. Create a parentcontroller that contains a user object and creates a
Build a Childcontroller to refer to this object:

functionfalse};}); App.controller (functionfunction= ' Ari Lerner ';};});

If we place the Childcontroller inside the Parentcontroller, the Childcontroller $scope
The parent scope of an object is the Parentcontroller $scope object. Based on the mechanism of the prototype inheritance, we can
The $scope object that accesses Parentcontroller in the scope.

<DivNg-controller= "Parentcontroller"><DivNg-controller= "Childcontroller"><aNg-click= "SayHello ()">Say Hello</a></Div>{{person}}</Div>

Controller of Angularjs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.