未捕获的错误:[$ injector:modulerr]无法实例化模块myModule

时间:2017-09-08 09:12:29

标签: html angularjs

我试图在视图中显示3个变量,但在控制台中出现错误

这里是script.js的代码

var myApp = angular
    .module("myModule",[])
    .controller("myController", function ($scope) {
        var employee = {
            firstName: "Sunil"
            lastName: "Bhatraju"
            gender: "Male"
        };

        $scope.employee = employee;
    });

这是demo.html的代码

<!doctype html>
<html ng-app="myModule">
<head>
    <script src="Scripts.js/script.js"></script>
    <script src="Scripts.js/angular.js"></script>
</head>
<body>
    <div ng-controller="myController">
<div>
    First Nmae: {{ employee.firstName }}
</div>
<div>
    Last Name : {{ employee.lastName }}
</div>
<div>
    Gender : {{ employee.gender }}
</div>
</div>

它在控制台上显示的错误是

1)未捕获的SyntaxError:意外的标识符

2)未捕获错误:[$ injector:modulerr]由于以下原因无法实例化模块myModule: 错误:[$ injector:nomod]模块&#39; myModule&#39;不可用!您要么错误拼写了模块名称,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。

here the image of my webstrom project

2 个答案:

答案 0 :(得分:3)

您需要加载angular.js参考,然后加载您的script.js

<script src="Scripts.js/angular.js"></script>
<script src="Scripts.js/script.js"></script>

并且您的对象应该在字段之间分隔逗号,

 var employee = {
                firstName: "Sunil",
                lastName: "Bhatraju",
                gender: "Male"
            };

<强>样本

&#13;
&#13;
var myApp = angular
    .module("myModule",[])
    .controller("myController", function ($scope) {
        var employee = {
            firstName: "Sunil",
            lastName: "Bhatraju",
            gender: "Male"
        };

        $scope.employee = employee;
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myModule">
    <div ng-controller="myController">
<div>
    First Nmae: {{ employee.firstName }}
</div>
<div>
    Last Name : {{ employee.lastName }}
</div>
<div>
    Gender : {{ employee.gender }}
</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

有多个错误

最后添加一个逗号

var employee = {
        firstName: "Sunil",
        lastName: "Bhatraju",
        gender: "Male"
};

两个首先添加angular lib

<script src="Scripts.js/angular.js"></script>
<script src="Scripts.js/script.js"></script>