【问题标题】:Inserting module with controller in function AngularJS在AngularJS函数中插入带有控制器的模块
【发布时间】:2018-05-01 16:19:50
【问题描述】:

为什么如果我将带有控制器的 angular 模块插入函数中,angularjs 会停止工作?

(function() {
      var app = angular.module("app", []);

      app.controller("c1", function($scope){

          $scope.name = "Hello World!";

      });  
 });

【问题讨论】:

  • 停止工作是什么意思?出现什么错误?
  • 控制台没有错误
  • 你的函数没有被调用!

标签: angularjs iife


【解决方案1】:

您忽略了IIFE 的代码(末尾的括号:())。

下面有一个修正了您的样本的 sn-p。

(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 的更多信息:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    相关资源
    最近更新 更多