您的工厂不在
$scope同一范围内,因此您无法访问您的。
尝试以下方法:
recipeApp.controller('recipeController', function ($scope, groceryInterface) { $scope.addToList = groceryInterface.addToList; $scope.clearList = groceryInterface.clearList; $scope.add = groceryInterface.add; $scope.addUp = groceryInterface.addUp;}recipeApp.factory('groceryInterface', function () { var factory = {}; factory.addToList = function (recipe) { this.groceryList.push(recipe); } factory.clearList = function() { var last = this.prevIngredients.pop(); }});另外,您可以尝试使用一种更加面向对象的方法:
recipeApp.controller('recipeController', function ($scope, groceryInterface) { $scope.groceryFunc = new groceryInterface($scope);}recipeApp.factory('groceryInterface', function () { function Factory ($scope) { this.$scope = $scope; } Factory.prototype.addToList = function (recipe) { this.$scope.groceryList.push(recipe); } Factory.prototype.clearList = function() { var last = this.$scope.prevIngredients.pop(); } return Factory;});


