javascript - 未定义的 Controller AngularJS

标签 javascript angularjs

我的模板正在加载,但出现 Controller 未定义的错误。 Controller 确实存在于我的源中定义的位置。这段代码有什么问题?

Error: [ng:areq] http://errors.angularjs.org/1.2.14/ng/areq?p0=ItemsController&p1=not%20aNaNunction%2C%20got%20undefined
    at Error (native)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js:6:450
    at xb 

这是我的 html:

<div ng-app="myApp">
  <ul ng-controller="ItemsController" class="nav">
    <input type="text" value="ItemName" ng-model="newItemName" placeholder="name of new item...">
    <button ng-click="addItem()">Add Me</button>
    <li ng-repeat="item in items.data" id="item{{item.id}}">
      <a href="#">{{item.title}}</a> 
      <a ng-click="deleteItem($index)" class="fa fa-trash-o"></a>
    </li>
  </ul>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular-route.js"></script>
<script type="text/javascript" src="js/te.js"></script>
<小时/>
'use strict';
var myApp = angular.module("myApp", []);
myApp.factory("items ", function() {
  var items = {};
  items.data = [];
  return items;
});
myApp.controller("ItemsController", function ItemsController($scope, items) {
  $scope.items = items;
  $scope.deleteItem = function(index) {
    items.data.splice(index, 1);
  }
  $scope.addItem = function(index) {
    items.data.push({
      id: $scope.items.data.length + 1,
      title: $scope.newItemName
    });
  }
});

最佳答案

我在您的代码中看到的唯一问题是以下行:

myApp.factory("items ", function() {

空格在工厂名称“items”中,并且您将作为项目注入(inject) Controller 。

这是工作 PLNKR您的代码,证明其工作正常。

很高兴为您提供帮助!

关于javascript - 未定义的 Controller AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29182493/

相关文章:

javascript - 当浏览器下载所需文件时,GIF 在启动画面中缓慢动画

javascript - AngularJS,添加滚动指令以拾取滚动底部不起作用

javascript - 使用 jquery 添加和删除多个元素

javascript - Ajax 在 PHP 页面上不工作

javascript - 为什么 bootstrap 在小屏幕上不起作用?

javascript - 平台工具\adb.exe : Comman d failed with exit code 3221226356

javascript - Angular - 更新模板中的徽章编号

java - 渲染 JSP 页面后设置 ng-model

javascript - IE7 和 IE8 不会触发 onsubmit javascript *before* 提交

javascript - 如何获取jquery中触发事件的对象?