简体   繁体   English

未定义的控制器AngularJS

[英]Undefined Controller AngularJS

My template is loading but I get an error that the controller is undefined. 我的模板正在加载,但是出现错误,即控制器未定义。 The controller does exist in my sources exactly at the location defined. 控制器确实存在于我定义的位置中。 What is wrong with this code? 此代码有什么问题?

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 

This is my html: 这是我的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
    });
  }
});

The Only Problem I see in your code is in following line: 我在您的代码中看到的唯一问题在以下行中:

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

space in the factory name "items " and you are injecting in your controller as items. 在工厂名称“ items”中添加 空格 ,您就将控制器作为项目注入。

Here is the working PLNKR of your code, proof its working fine. 这是您代码的有效PLNKR ,证明其工作正常。

Happy Helping! 快乐的帮助!

I have faced this problem many a times and usually this is caused when you have not included your js in the template. 我已经多次遇到此问题,通常是由于您未在模板中包含js引起的。

If its not the case, then if you could share your some part of code, then only anybody will be able to answer it properly. 如果不是这种情况,那么如果您可以共享部分代码,那么只有任何人都可以正确回答它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM