use*_*664 8 javascript angularjs
我有一个要求我应该写工厂的要求.该工厂应包含3个函数init,save和delete
我应该从控制器调用init函数.此函数返回一个对象.该对象具有执行添加和删除功能的功能.
我怎么能实现这个目标?
以下是我的代码,它成功执行init函数,但是当我尝试使用添加或删除时返回的对象时,它表示对象为空
angularApp.factory('SomeFactory', function(){
var client = new Client(); // this client is defined in another javascript file
// this is the object which we should return
var clientReady = function () {
var cv = client.GetVersion();
showIDs();
};
return {
initClient:function(requiredUID){
client.setAttribute("clientReadyCallback",clientReady);
}//,
};
var add = function () {
client.someapi;
};
var delete = function () {
client.someapi;
};`
});
in controller i call the below calls
SomeFactory.initClient("username");
SomeFactory.add();// throws error
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现这一目标?
m59*_*m59 19
首先,你不是回工厂,而是服务.它是一个创建服务的工厂,因此请调整您的命名约定,如下所示:app.factory('someService'
您的代码很乱并且有错误,因此我将向您展示返回服务对象的基础知识.
app.factory('someService', function() {
var someService = { //build this object however you want
add: function() {
},
save: function() {
}
};
return someService; //return the object
}); //end factory
Run Code Online (Sandbox Code Playgroud)
在你的控制器中: someService.add();
| 归档时间: |
|
| 查看次数: |
16932 次 |
| 最近记录: |