Angularjs NG-BOOK代码无法正常工作

时间:2016-10-13 04:11:04

标签: javascript angularjs

代码位于此链接中:http://jsbin.com/gekilazilo/edit?html,js,output

我将代码复制到我的sublime中,然后在Chrome浏览器中运行。控制台信息如下...... " angular.js:7861错误:[ng:areq] Argument' MyController'不是一个功能,未定义 http://errors.angularjs.org/1.2.0-rc.2/ng/areq?p0=MyController&p1=not%20a%20function%2C%20got%20undefined "

我不知道为什么当我在自己的笔记本电脑上试用时,本书中的代码总是报错。

这是我运行的确切代码。 对于HTML:

<!DOCTYPE html>
<html lang="en" ng-app>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>    

  <div ng-controller="MyController">
    <input ng-model="expr"
            type="text"
            placeholder="Enter an expression" />
    <h2>{{ parsedExpr }}</h2>
  </div>
  </body>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.js"></script>
<script src="Controller2.js">
</html>

for Controller2.js:

function MyController($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);
       }
});
};

1 个答案:

答案 0 :(得分:0)

您遗失了一些关闭});的监视表达式

function MyController($scope, $parse) {
       $scope.person = {
       name: "Ari Lerner"
   };
   $scope.$watch('expr', function(newVal, oldVal, scope) {
       if (newVal !== oldVal) {
         var parseFun = $parse(newVal);
         scope.parsedExpr = parseFun(scope);
       }
  });
};