angular.js $parse的用法demo

《angular权威教程》中的$parse的用法Page20页中给了一个demo,无法运行出正确结果,后台也没有报错

做了如下更改后,可正确运行,但目前无法确定自己的使用方法是否正确。仅供各位看官参考

<!doctype html>

<html ng-app="myApp">
<head>
  <title>Parse Expression Example</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script>
</head>
<body>


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


  <script>
    angular.module('myApp', [])
    .controller('MyController',
    ['$scope', '$parse', function($scope, $parse) {
      $scope.$watch('expr', function(newVal, oldVal, s) {
        if (newVal !== oldVal) {
          var parseFun = $parse('expr');
          console.log(parseFun);
          s.parsedExpr = parseFun(s);
        }
      });
    }]);
  </script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/Lambert0320/article/details/70156336