AngularJS 未捕获错误 : No module: myApp

标签 angularjs

AngularJS:使用 angular.module 和 VM

我已经如下定义了我的 Angular 模块,并希望按照指南使用 VM。不确定我在这里做错了什么,但它在控制台中给我错误:

Uncaught Error: No module: myApp

这是我的代码:

<div ng-controller="Ctrl">
    Hello, {{vm.name}}!
</div>
var app = angular.module('myApp');

app.controller('Ctrl', ['$scope', '$http',
  function ($scope, $http) { 
    var vm = this;
    vm.name = "John";
}]);

这是我的 jsfiddle:

https://jsfiddle.net/dn7pkuf8/

最佳答案

首先,您需要在声明模块时添加一个空数组(或包含您的依赖项的数组)。如果没有数组,您将尝试获取名为 myApp 的模块。

var app = angular.module('myApp', []);

然后您需要将应用名称添加到NgApp View 中的指令,否则 Angular 应用程序需要 Bootstrap 。

这样试试

<body ng-app="myApp">
  <div ng-controller="Ctrl as vm">
     Hello, {{vm.name}}!
  </div>
</body>

关于AngularJS 未捕获错误 : No module: myApp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50933516/

相关文章:

javascript - 将 ui-router 与 View 转换一起使用

javascript - 如何更改图像的名称?

javascript - 如何防止使用 Angular JS 在输入键时提交表单?

javascript - AngularJS - 为什么在指令中使用匿名 Controller ?

javascript - Angular JS : binding in ng-show not working

javascript - 使用AngularJS select选择两组信息

javascript - Angular JS 以树状格式呈现 JSON

javascript - 使用 Javascript AngularJS 逐一单击后在框中显示元素

angularjs - 结合使用 Bootstrap for Angular 和 Material design for Angular

html - td relative 中的过渡元素使表格闪烁