我创建了一个Angular工厂,然后我在控制器中使用它。工厂包含我试图在我的控制器中使用的delete
方法,但是,我看到一个错误,指出index
未定义。以前,在我有一个工厂之前,当一切都在控制器中时,这个功能起作用,但现在不再定义index
。我在这里读了很多答案,但找不到答案。
我的代码如下所示:
js
app.factory('addressFactory', function(){
var addressFactory = {};
addressFactory.addressBook = [];
addressFactory.deleteAddress = function(index) {
addressFactory.addressBook.splice(index, 1);
localStorage.setItem('addressBook', JSON.stringify(messages.addressBook));
}
return addressFactory;
})
.controller('testCtrl', ['$uibModal', 'addressFactory', function ($uibModal, addressFactory) {
this.addressBook = addressFactory.addressBook;
this.deleteAddress = addressFactory.deleteAddress(index);
}]);
HTML
<div ng-repeat="contact in ctrl.addressBook track by $index">
<p>{{contact.name}}</p>
<button ng-click="ctrl.deleteAddress($index)">Delete Contact</button>
</div>
答案 0 :(得分:1)
更改此
httpd -k restart
到此:
this.deleteAddress = addressFactory.deleteAddress(index);