浏览器无法解析AngularJS代码

时间:2015-09-29 16:15:22

标签: javascript angularjs browser

我在AngularJS中编写了一个简单的程序,在脚本标记中包含了缩小的角度JS,但是浏览器无法解析角度JS代码。我有其他程序或多或少相同的代码,他们工作正常。

我是否遗失/忽视任何事情?

MVC示例

<!DOCTYPE html>
<html ng-app>
    <head>
        <title>MVC Example</title>
        <script src="js/angular.min.js"></script>
        <script>
            function MyFirstCtrl($scope) {
                var employees = ['Catherine Grant', 'Monica Grant', 'Christopher Grant', 'Jennifer Grant'];
                $scope.ourEmployees = employees;
            }
        </script>
    </head>
        <body ng-controller='MyFirstCtrl'>
        <h2>Number of Employees: {{ourEmployees.length}}</h2>
    </body>
</html>

浏览器输出:

Number of Employees: {{ourEmployees.length}}

3 个答案:

答案 0 :(得分:4)

您需要创建一个与angular.module("foo", []); angular.controller("MyFirstCtrl", MyFirstCtrl); <html ng-app=foo> 一起使用的模块。

<div>

答案 1 :(得分:0)

您需要启动应用并以正确的形式添加控制器:

<script>
var app = angular.module("app", []);
app.controller('MyFirstCtrl', ['$scope', function ($scope) {
    var employees = ['Catherine Grant', 'Monica Grant', 'Christopher Grant', 'Jennifer Grant'];
    $scope.ourEmployees = employees;
}]);
<script>

答案 2 :(得分:0)

在一些R&amp; D之后我终于找到了v1.2和v1.3 +的解决方案

v1.2

<!DOCTYPE html>
<html ng-app=foo>
    <head>
        <title>MVC Example</title>
        <script src="js/angular.min-1.2.js"></script>
        <script>
            angular.module("foo", []);
            angular.controller("MyFirstCtrl", MyFirstCtrl);

            var employees = ['Catherine Grant', 'Monica Grant', 'Christopher Grant', 'Jennifer Grant'];

            function MyFirstCtrl($scope) {
                var employees = ['Catherine Grant', 'Monica Grant', 'Christopher Grant', 'Jennifer Grant'];
                $scope.ourEmployees = employees;
            }
        </script>
    </head>
        <body ng-controller='MyFirstCtrl'>
        <h2>Number of Employees: {{ourEmployees.length}}</h2>
    </body>
</html>

v1.3 +向前

<!DOCTYPE html>
<html ng-app=foo>
    <head>
        <title>MVC Example</title>
        <script src="js/angular.min-1.4.js"></script>
        <script>
            var employees = ['Catherine Grant', 'Monica Grant', 'Christopher Grant', 'Jennifer Grant'];
            var foo = angular.module("foo", []);
            foo.controller("MyFirstCtrl", function ($scope) {
                $scope.ourEmployees = employees;
            });
        </script>
    </head>
        <body ng-controller='MyFirstCtrl'>
        <h2>Number of Employees: {{ourEmployees.length}}</h2>
    </body>
</html>