简单的AngularJS示例(错误:[$injector:nomod],错误:[$sinjector:modulerr]

Simplistic AngularJS example (Error: [$injector:nomod], Error: [$injector:modulerr])

本文关键字:错误 sinjector modulerr nomod injector 示例 简单 AngularJS      更新时间:2023-09-26

我正在尝试构建简单明了的(对我来说)单小提琴角度JS示例,并编写了类似的东西

var myApp = angular.module('myApp');
    
var myControllers = angular.module('myControllers', []);
myControllers
  .controller('MyController', ['$scope', function ($scope) {
    $scope.watts = 735.5;
  }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
    <div ng-controller="MyController">
      		Watts:<input type="text" ng-model="watts"><br/>
    </div>
</body>

不幸的是,它不起作用,并抛出了一个例外。

如果您试图将控制器放在一个单独的模块中,则需要将其列为主应用程序的依赖项。

var myControllers = angular.module('myControllers', []);
var myApp = angular.module('myApp', ['myControllers']);
myControllers
  .controller('MyController', ['$scope',
    function($scope) {
      $scope.watts = 735.5;
    }
  ]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
  <div ng-controller="MyController">
    Watts:
    <input type="text" ng-model="watts">
    <br/>
  </div>
</body>