得到错误参数' MainController'不是一个功能,未定义

时间:2015-10-15 15:43:07

标签: javascript angularjs items

于伊!我的代码中存在一些问题:

这是我的index.html文件:

<body ng-app="myApp" ng-controller="MainController">

        <nav class="navbar navbar-default">
            <div class="container-fluid">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                  <a class="navbar-brand" href="#">diyetSahovatKafe</a>
                </div>
            </div>
        </nav>
        <div class="main">
            <div class="container">
                <div class="row">
                    <div class="col-md-5">
                      <ul>
                        <li ng-repeat="item in items.data" id="item{{item.id}}">
                          <a href="#">{{item.title}}</a> 
                          <img ng-src="{{ item.image }}" />
                          <button ng-click="addItem(item)">Add Me</button>
                        </li>
                      </ul>
                      <p>Cart:</p>
                      <ul>
                        <li ng-repeat="item in cart">
                          <a href="#">{{item.title}} | Count: {{item.count}}</a> 
                          <a ng-click="deleteItem(item)" class="delete-item">X</a>

                        </li>
                      </ul>
                    </div>
                </div>
            </div>
        </div>

结束这是我的app.js文件:

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

和控制器文件:

app.controller('MainController', ['$scope', function ($scope, items) {

$scope.items = items;
$scope.cart = [];

$scope.deleteItem = function(item) {
    var cart = $scope.cart;
    var match = getMatchedCartItem(item);
    if (match.count > 1) {
      match.count -= 1;
      return;
    }
    cart.splice(cart.indexOf(item), 1);
  }



$scope.addItem = function(item) {
    var match = getMatchedCartItem(item);
    if (match) {
      match.count += 1;
      return;
    }
    var itemToAdd = angular.copy(item);
    itemToAdd.count = 1;
    $scope.cart.push(itemToAdd);
  }


  function getMatchedCartItem(item) {
    return $scope.cart.find(function(itm) {
      return itm.id === item.id
    });

  }

}]);

.factory = ("items", function() {
  var items = {};
  items.data = [{
    id: 1,
    title: "Item 1",
    image: "img/item01.jpg"
  }, {
    id: 2,
    title: "Item 2",
    image: "img/item02.jpg"
  }, {
    id: 3,
    title: "Item 3",
    image: "img/item03.jpg"
  }, {
    id: 4,
    title: "Item 4",
    image: "img/item04.jpg"
  }];
  return items;
});

运行代码时没有任何内容,请检查我的代码并解释我的错误,谢谢!

1 个答案:

答案 0 :(得分:0)

  1. 您在:

    中有额外的;=个字符
    }]);  // end of MainController
    
    .factory = ("items", function() {
    
  2. 您忘了注入items

    app.controller('MainController', ['$scope', function ($scope, items) {
    
  3. 那就是说,我怀疑你有另一个错误,这在这里是看不到的,因为有了可见的东西,你应该得到像Uncaught SyntaxError: Unexpected token .这样的东西,用于在分号后写一个点。