Angular Code没有执行

时间:2018-06-04 13:09:51

标签: angularjs

我正在学习AngularJS,以此作为我的Python / Django项目的前端。我刚刚写了一个简单的AngularJS页面,如下所示。

代码的预期输出为 Joe已有35年历史。

虽然代码的实际输出 {{person.name}}是{{person.age}}年。

代码如下。

<html>

<body ng-app="scrumboard.demo">
  <div ng-controller="ScrumboardController">
    <p>{{ person.name }} is {{ person.age }} years old.</p>
  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.10/angular.min.js"></script>

  <script>
    (function() {
        'use strict';
        angular.module('scrumboard.demo', [])
          .controller('ScrumboardController', ['$scope', ScrumboardController]);

        function ScrumboardController($scope) {
          $scope.person = {
            name = 'Joe',
            age = 35
          };
        }());
  </script>

</body>

</html>

3 个答案:

答案 0 :(得分:2)

您的 object声明错误,

$scope.person = {
   name = 'Joe',
   age = 35
};

name = 'Joe',应为name: 'Joe',age = 35应为age: 35

所以,

$scope.person = {
   name: 'Joe',
   age: 35
};

&#13;
&#13;
<html>

<body ng-app="scrumboard.demo">
  <div ng-controller="ScrumboardController">
    <p>{{ person.name }} is {{ person.age }} years old.</p>
  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.10/angular.min.js"></script>

  <script>
    (function() {
        'use strict';
        angular.module('scrumboard.demo', [])
          .controller('ScrumboardController', ['$scope', ScrumboardController]);

        function ScrumboardController($scope) {
          $scope.person = {
            name: 'Joe',
            age: 35
          };
        }
     }());
  </script>

</body>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

更改此

$scope.person = {
        name = 'Joe',
        age = 35
      };

$scope.person = {
        name: 'Joe',
        age: 35
      };

<html>

<body ng-app="scrumboard.demo">
  <div ng-controller="ScrumboardController">
    <p>{{ person.name }} is {{ person.age }} years old.</p>
  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.10/angular.min.js"></script>

  <script>
    (function() {
        'use strict';
        angular.module('scrumboard.demo', [])
          .controller('ScrumboardController', ['$scope', ScrumboardController]);

        function ScrumboardController($scope) {
          $scope.person = {
            name: 'Joe',
            age: 35
          };
        }
     }());
  </script>

</body>

</html>

答案 2 :(得分:0)

谢谢Sravan和Jag。 Asa正确地指出问题在于我通过改变

来修复的对象符号
name = 'Joe',
age = 35

要     姓名:&#39; Joe&#39;,     年龄:35岁

然而,在这样做之后,代码仍然无效。经过进一步审查后,我发现我错过了一个大括号&#39;}&#39;这里

(function() {
    'use strict';
    angular.module('scrumboard.demo', [])
      .controller('ScrumboardController', ['$scope', ScrumboardController]);

    function ScrumboardController($scope) {
      $scope.person = {
        name = 'Joe',
        age = 35
      };
    }());

我改变了这个

(function() {
    'use strict';
    angular.module('scrumboard.demo', [])
      .controller('ScrumboardController', ['$scope', ScrumboardController]);

    function ScrumboardController($scope) {
      $scope.person = {
        name = 'Joe',
        age = 35
      };
      }
    }());

并且有效。