我的模板正在加载,但出现 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/