我一直在玩角度,并希望使用require将其添加到应用程序中。我有一个名为Test的类,它包含在Angular的require中。问题是,如果我没有直接在angular语句的angular语句下初始化angular.module,我会收到错误说明未捕获的错误:[$ injector:modulerr]
<!DOCTYPE html>
<html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.3/require.min.js"></script>
<script>
require.config({
paths: {
"angular": "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min"
}
});
</script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
Name: <input ng-model="name">
<h1>You entered: {{name}}</h1>
</div>
<script>
require(['angular'], function () {
var Test = class Test {
constructor() {
this.setAngular();
};
setAngular() {
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name = "John Doe";
});
}
}
});
var test = new Test();
</script>
</body>
</html>
&#13;
但是,如果我将require语句向下移动到setAngular函数内部,如下所示:
setAngular() {
require(['angular'], function () {
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name = "John Doe";
});
});
}
然后一切正常。我在这里错过了什么吗?