我试图将视图函数添加到Angular $ resource。我通过原型将其添加到$ resource中,但由于某种原因,原型函数中的“ this”引用不正确,因此所有属性均未定义。奇怪的是,尽管在console.log中,它似乎具有正确返回所需的所有属性。
http://plnkr.co/edit/YsTlAztjEKjn3piQAem2?p=preview
app.factory("Now", function($resource) {
var Now = $resource("http://date.jsontest.com/");
Now.prototype.$dateTime = function() {
console.log("2", this); // this has date and time properties, good
return this.date + " " + this.time;
};
return Now;
});
app.controller("TestController", function(Now) {
var now = new Now();
now.$get();
console.log("1", now); // prototype has the $dateTime function!
console.log("3", now.$dateTime()); // but it returns undefined, bad
});
实际上,您的错误是在资源返回数据之前调用$ dateTime。
看到这个pl
我上面的答案似乎起作用的唯一原因是正在插值,当资源最终返回时,datetime函数称为AGAIN。但是,如果您将代码保持不变,它仍然会失败
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句