在函数 AngularJS 中插入带有控制器的模块
Inserting module with controller in function AngularJS
为什么如果我将带有控制器的 angular 模块插入到函数中,angularjs 停止工作?
(function() {
var app = angular.module("app", []);
app.controller("c1", function($scope){
$scope.name = "Hello World!";
});
});
您忽略了 IIFE 的代码(末尾的括号: ()
)。
下面是您的样本更正后的片段。
(function() {
var app = angular.module("app", []);
app.controller("c1", function($scope){
$scope.name = "Hello World!";
});
})(); //<< -- here!! See the closing ()
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="c1">
{{name}}
</div>
您可以在此处阅读有关 IIFE 的更多信息:
- What is the (function() { } )() construct in JavaScript?
- https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
为什么如果我将带有控制器的 angular 模块插入到函数中,angularjs 停止工作?
(function() {
var app = angular.module("app", []);
app.controller("c1", function($scope){
$scope.name = "Hello World!";
});
});
您忽略了 IIFE 的代码(末尾的括号: ()
)。
下面是您的样本更正后的片段。
(function() {
var app = angular.module("app", []);
app.controller("c1", function($scope){
$scope.name = "Hello World!";
});
})(); //<< -- here!! See the closing ()
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="c1">
{{name}}
</div>
您可以在此处阅读有关 IIFE 的更多信息:
- What is the (function() { } )() construct in JavaScript?
- https://en.wikipedia.org/wiki/Immediately-invoked_function_expression