【问题标题】:AngularJS: Argument 'Controller as short' is not a function, got undefinedAngularJS:参数'Controller as short'不是函数,未定义
【发布时间】:2014-12-03 16:55:15
【问题描述】:

我很难找出问题所在。

为什么这段代码(index.html)会运行

<!DOCTYPE html>
<html ng-app="gemStore">
  <head>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script>
    <script type="text/javascript" src="js/app.js"></script>
  </head>
  <body>
    <div class="list-group-item" ng-repeat="product in store.products">
      <section ng-controller="TabController as tab">
      </section>
    </div>
  </body>
</html>

同时

<!DOCTYPE html>
<html ng-app="gemStore">
  <head>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script>
    <script type="text/javascript" src="js/app.js"></script>
  </head>
  <body>
      <section ng-controller="TabController as tab">
      </section>
  </body>
</html>

产生错误:'错误:参数'TabController as tab'不是函数,在错误(本机)处未定义'

app.js:

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

  app.controller('StoreController', function() {
    this.products = gems;
  });

  app.controller("TabController", function() {
    this.tab = 1;

    this.isSet = function(checkTab) {
      return this.tab === checkTab;
    };

    this.setTab = function(setTab) {
      this.tab = setTab;
    };
  });

  var gems = [
    {
      data : 'data'
    }
  ];
})();

非常感谢您!

【问题讨论】:

  • 查看您的版本中功能的可用性。

标签: javascript angularjs


【解决方案1】:

您的问题是因为您引用了 Angular 1.0.6 而引起的。 “Controller as”语法在 Angular 1.2.0 之前版本中不可用。如果您将 1.0.6 更改为 1.2.0,它应该可以正常工作。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 2015-04-01
  • 2013-10-24
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 2015-09-02
相关资源
最近更新 更多