AngularJS Note Series (iv) Controllers and expressions

Source: Internet
Author: User

Controller: In Angular the controller is a function used to add additional functionality to the scope. We use it to set the initial state of the scope object and add custom behavior.

How to use: var app = Angualr.module (' app ', []);

App.controller (' TestController ', function ($scope) {

Do sth here
})

Controller nesting: A parent scope exists throughout the application, and all scopes inherit from the prototype, except for the orphaned scope. They all have access to the parent scope.

By default, when AngularJS cannot find a property in the current scope, it looks up and goes back to $rootscope, and if it is not found, the program continues to run, but the view cannot be updated.

Best practice: Dapper, you should not manipulate DOM and data too much in the controller. Complex business logic should be put into services and views.

Expression: In Angular, the way we bind variables to $scope through {{}} is essentially an expression. When listening with $watch, Angular will perform operations on functions or expressions.

Parse expression: Angular parses an expression by $parse the internal service, $interpolate the service returns a function that is used to operate the expression in a particular context.

<DivNg-controller= "Mycontroller">       <inputNg-model= "to"type= "Email"placeholder= "Recipient" />       <textareaNg-model= "Emailbody"></textarea>       <Pre>{{Previewtext}}</Pre></Div>

Angular.module (' myApp ', [])       . Controller (function($scope, $interpolate) {//  function(body) {           if  (body) {             var template = $ Interpolate (body);              =               Template ({to: $scope. to});           

AngularJS Note Series (iv) Controllers and expressions

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.