angularjs权威教程阅读笔记

1. 解析angularjs表达式

注入$pase服务,设置字符串表达式,获取表达式的值用于返回前台。

<body>

  <div ng-controller="MyController">
    <input ng-model="expr"
            type="text"
            placeholder="Enter an expression" />
    <div>{{ parsedExpr }}</div>
  </div>

  <script>
    // open this example and type person.name into the test field
    angular.module('myApp', [])
    .controller('MyController',
    ['$scope', '$parse', function($scope, $parse) {

      $scope.person = {
        name: "Ari Lerner"
      };

      $scope.$watch('expr', function(newVal, oldVal, scope) {
        if (newVal !== oldVal) {
          // Let's set up our parseFun with the expression
          var parseFun = $parse(newVal);
          // Get the value of the parsed expression, set it on the scope for output
          scope.parsedExpr = parseFun(scope);
        }
      });
    }]);
  </script>

</body>

 

posted @ 2017-04-07 11:39  yangfei969  阅读(234)  评论(0编辑  收藏  举报