在 angular 中分离控制器逻辑和工厂逻辑
separate controller logic and factory logic in angular
我不想将我所有的代码都放在一个巨大的控制器中,所以我试图对其进行一些重组,但我 运行 遇到了这个错误
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));
变化:
/*
主要 - 调用控制器和工厂的地方
*/
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')
}
}
});
服务:
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);
};
}
}
)
我不想将我所有的代码都放在一个巨大的控制器中,所以我试图对其进行一些重组,但我 运行 遇到了这个错误
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));
变化:
/* 主要 - 调用控制器和工厂的地方 */
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')
}
}
});
服务:
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);
};
}
}
)