【问题标题】:Angularjs Argument is not a function got stringAngularjs参数不是一个函数得到字符串
【发布时间】:2013-06-17 15:38:26
【问题描述】:

我是 angularjs 的新手。我正在构建一个应用程序,我的主页将包含一个帖子列表。我想单独使用 angularjs 来为该页面进行两种方式的绑定。我从一个示例页面开始,但在这里遇到了问题。

我的示例页面。我只对这个 div 使用 ng-app,因为我不希望 angularjs 与页面的任何其他方面进行交互。

<div ng-app="posts">
  <ul class="post_list" ng-controller="PostController">
    <li ng-repeat="post in posts">
    {{post.title}}
    {{post.description}}
    </li>
  </ul>
</div>

我有一个 app.js 文件,其中有

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

还有一个带有

的 postcontroller.js 文件
(function(angular, app) {
  // Define the Controller as the constructor function.
  console.log(app);
  console.log(angular);
  app.controller("PostController",["$scope"], function($scope) {
    $scope.posts = [
      {"title":"asdf", "description":"describe"},
      {"title":"second one", "description":"second describe"},
    ];
  });
})(angular, app);

当我加载我的主页时。我得到了

错误:参数“PostController”不是函数。得到字符串 [googlecdn 路径]angular.min.js:16

我在这里缺少什么。请帮助我,因为我很困惑。我是 angularjs 和 javascript 的新手。

【问题讨论】:

    标签: javascript angularjs modularity


    【解决方案1】:

    问题在于您声明控制器的方式。您应该如下所示:

    app.controller("PostController",["$scope", function($scope) {
        $scope.posts = [
          {"title":"asdf", "description":"describe"},
          {"title":"second one", "description":"second describe"},
        ];
      }]);
    

    请注意,第二个参数是一个数组,其中包含$scope 字符串和函数作为数组元素。这是用于编写缩小安全代码的正确语法。

    【讨论】:

    • 在这些更改之后运行良好。你能给我指出一些资源来解释为什么这种语法适用于最小化吗?感谢您的帮助。
    • @R.A.J 看看docs.angularjs.org/guide/di中的“依赖注解”部分
    • 缩小会破坏函数和参数名称,但不会破坏字符串文字。
    • 这个答案对我有帮助,但我的问题是关闭的“]”不是在控制器的末尾,而是在注射的末尾。哎呀。 :)
    猜你喜欢
    • 2014-06-29
    • 2016-11-30
    • 2014-05-18
    • 1970-01-01
    • 2013-10-06
    • 2015-05-10
    • 2023-03-31
    • 1970-01-01
    • 2023-02-11
    相关资源
    最近更新 更多