Angularjs Expression-Expression

Source: Internet
Author: User

Later, we will talk about angularjs DI (Dependency Injection). Here we will introduce the expression about angularjs. Expression refers to a small part of javascript code, which is usually used to bind binding. For example: {expression }}. In angularjs, $ parse service is used for parsing.

$ Parse usage: $ parse (expression );

The following expressions are valid expressions in angularjs:

Angularjs expression vs. javascript expression

Angularjs view expressions are a bit like javascript expressions, but they are not parsed and executed using javascript expressions eval (). The differences between them and javascript expressions are as follows:

Note: angularjs expressions can be parsed and executed using the $ eval () method.

 

Demo

Html:

 

 
 
  1. <!doctype html> 
  2.  
  3.  
  4.  
  5. <script src="http://code.angularjs.org/angular-1.0.2.min.js"></script> 
  6.  
  7. <script src="script.js"></script> 
  8.  
  9.  
  10. <body> 
  11.  
  12. <div ng-controller="Cntl2" class="expressions"> 
  13.  
  14. Expression:  
  15.  
  16. <input type='text' ng-model="expr" size="80"/> 
  17.  
  18. <button ng-click="addExp(expr)">Evaluate</button> 
  19.  
  20. <ul> 
  21.  
  22. <li ng-repeat="expr in exprs"> 
  23.  
  24. [ <a href="" ng-click="removeExp($index)">X</a> ]  
  25.  
  26. <tt>{{expr}}</tt> =&gt; <span ng-bind="$parent.$eval(expr)"></span> 
  27.  
  28. </li> 
  29.  
  30. </ul> 
  31.  
  32. </div> 
  33.  
  34. </body> 
  35.  
  36.  

Js:

 
 
  1. function Cntl2($scope) {  
  2.  
  3. var exprs = $scope.exprs = [];  
  4.  
  5. $scope.expr = '3*10|currency';  
  6.  
  7. $scope.addExp = function(expr) {  
  8.  
  9. exprs.push(expr);  
  10.  
  11. };  
  12.  

 

$ Scope. removeExp = function (index ){

Exprs. splice (index, 1 );

};

}

Jsfiddle Demo: http://jsfiddle.net/whitewolf/yduLt/1/

Attribute execution

All expression execution in angularjs depends on one action on the scope, which is different from the global scope of the window in javascript. if you want to reference a global window, this must be dependent on the DI feature referenced in the previous section $ window service. example:

Html:

 
 
  1. <!doctype html> 
  2.  
  3.  
  4.  
  5. <script src="http://code.angularjs.org/angular-1.0.2.min.js"></script> 
  6.  
  7. <script src="script.js"></script> 
  8.  
  9.  
  10. <body> 
  11.  
  12. <div class="example2" ng-controller="Cntl1"> 
  13.  
  14. Name: <input ng-model="name" type="text"/> 
  15.  
  16. <button ng-click="greet()">Greet</button> 
  17.  
  18. </div> 
  19.  
  20. </body> 
  21.  
  22.  

Js:

 
 
  1. function Cntl1($window, $scope){  
  2.  
  3. $scope.name = 'World';  
  4.  
  5.    
  6.  
  7. $scope.greet = function() {  
  8.  
  9. ($window.mockWindow || $window).alert('Hello ' + $scope.name);  
  10.  
  11. }  
  12.  
  13. }  
  14.  

Jsfiddle Demo: http://jsfiddle.net/whitewolf/MF2Ku/1/

Compatible execution

As mentioned above: angularjs expression computation is compatible with processing null and undefined, and will not throw any exception because it will handle the reality in view display, while javascript will throw the NullPointerExceptions exception. For example, expression

{A. B. c}. The javascript code with the same effect will be {(a |{}). B |{}). c }}. Uncontrolled Process

In angularjs expressions, there will be no conditions, loops, and throw control flow statements. Angularjs, as mvc or close to pm mode, requires that the presentation layer logic be included in the controller, rather than the view, and the view should be passive enough. In expression mode, we try our best to extract the unchanged presentation logic of the presentation layer and a variety of easy-to-change UI views to facilitate automated testing and building.

Filter Filters)

Data is only used as a persistence storage domain model presentation layer or actually a view model viewmodel). When presented to users, it is often more user-friendly, such as time, number, currency format localization,

For example, name | uppercase, 123 | number: 2, 3*10 | currency.

Filters supports chained writing. How to use a pipeline model in the same language as powershell or other operating system shell, such as value | filter1 | filter2.

 

For other resources, refer to other angularjs documents in this blog or official angularjs documents http://angularjs.org /.

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.