我不想将我的所有代码都放在一个巨大的控制器中,所以我试图稍微重组一下,但是我遇到了这个错误
Argument 'protocolController' is not a function, got undefined
我有我的工厂
/*
FACTORY THAT HANDLES ALL REQUESTS.
*/
function asyncFactory() {
var factory = this;
factory.list = [];
this.update = function(restUrl) {
return $http.get(restUrl)
.success(function(data){
factory.list = data;
})
.error(function(data){
console.log('Error: ' + data);
});
};
this.add = function(formData, restUrl){
$http.post(restUrl, JSON.stringify(formData))
.success(function(data){
console.log('Success: ' + data);
})
.error(function(data){
console.log('Error: ' + data);
});
};
this.remove = function(value, restUrl){
// get index of clicked object
var index = this.protocolList.indexOf(value);
// Remove object with given index from array
factory.list.splice(index, 1);
var url = restUrl + value.id;
// delete it from DB
$http['delete'](url);
console.log('Deleted object with ID ' + value.id);
};
// Change existing object
this.change = function(value, restUrl) {
// get index of clicked object
var index = this.protocolList.indexOf(this.activeObject);
var url = restURL + value.id + '/' + value.name;
console.log('url: ' + url)
// change it before store it to DB
$http['put'](url);
console.log('Changed object with ID ' + this.activeObject.id);
};
}
和我使用工厂的控制器
/*
PROTOCOL CONTROLLER
*/
function protocolController(asyncFactory){
var controller = this;
controller.list = asyncFactory.list;
controller.formData = {};
this.getUpdatedList = function() {
asyncFactory.update('../protocols')
.then(function(data){
controller.list = data;
});
};
this.addData = function(formData){
asyncFactory.add(formData, '../protocols');
};
this.removeData = function(value){
asyncFactory.remove(value, '../protocols');
};
this.editData = function(value){
asyncFactory.change(value, '../protocols')
}
}
并称之为:
/*
MAIN - WHERE THE CONTROLLER AND FACTORY IS CALLED
*/
var app = angular.module('TransactionMonitoring', [])
.controller('protocolController', protocolController(asyncFactory));
答案 0 :(得分:1)
更改
/ * 主要 - 调用控制器和工厂的地方 * /
var app = angular.module('TransactionMonitoring', [])
.controller('protocolController', protocolController(asyncFactory));
要,强>
/ * 主要 - 调用控制器和工厂的地方 * /
var app = angular.module('TransactionMonitoring', [])
.controller('protocolController', function(asyncFactory){
var controller = this;
controller.list = asyncFactory.list;
controller.formData = {};
this.getUpdatedList = function() {
asyncFactory.update('../protocols')
.then(function(data){
controller.list = data;
});
};
this.addData = function(formData){
asyncFactory.add(formData, '../protocols');
};
this.removeData = function(value){
asyncFactory.remove(value, '../protocols');
};
this.editData = function(value){
asyncFactory.change(value, '../protocols')
}
}
});
SERVICE:
app.service('asyncFactory',function ($http) {
this.update = function(restUrl) {
return $http.get(restUrl)
.success(function(data){
factory.list = data;
})
.error(function(data){
console.log('Error: ' + data);
});
};
this.add = function(formData, restUrl){
$http.post(restUrl, JSON.stringify(formData))
.success(function(data){
console.log('Success: ' + data);
})
.error(function(data){
console.log('Error: ' + data);
});
};
this.remove = function(value, restUrl){
// get index of clicked object
var index = this.protocolList.indexOf(value);
// Remove object with given index from array
factory.list.splice(index, 1);
var url = restUrl + value.id;
// delete it from DB
$http['delete'](url);
console.log('Deleted object with ID ' + value.id);
};
// Change existing object
this.change = function(value, restUrl) {
// get index of clicked object
var index = this.protocolList.indexOf(this.activeObject);
var url = restURL + value.id + '/' + value.name;
console.log('url: ' + url)
// change it before store it to DB
$http['put'](url);
console.log('Changed object with ID ' + this.activeObject.id);
};
}
}
)