[英]Provider 'xx' must return a value from $get factory method in AngularJs
I have written an angularjs
factory as below 我写了一个
angularjs
工厂如下
module.factory('LogService', function () {
function log(msg) {
console.log("Rahkaran:" + new Date() + "::" + msg);
}
return
{
log: log
};
});
But I kept getting this error 但我一直收到这个错误
Provider 'LogService' must return a value from $get factory method
I googled about the error and I couldn't find any solution. 我用谷歌搜索了错误,我找不到任何解决方案。
Coincidentally I changed the return
statement to this 巧合的是我把
return
语句更改为了
return{
log: log
};
And error is gone!! 错误消失了!!
Is there any differences between having {
in front of return
or at the next line? {
return
前或下一行之间是否有任何区别?
This is called Automatic semicolon insertion 这称为自动分号插入
The return statement is affected by automatic semicolon insertion (ASI). return语句受自动分号插入(ASI)的影响。 There is no line terminator
;
没有线路终结器
;
between the return keyword and the expression allowed. 返回关键字和允许的表达式之间。
return
a + b;
// is transformed by ASI into
return;
a + b;
So you must insert {
in front of return and Not at the next line. 因此,您必须在返回前插入
{
而不是在下一行。
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return 参考: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return
In your case there will be always a non-undefined value returned. 在您的情况下,将始终返回未定义的值。 But in other cases the issue might be also that you do return null or undefined value from the factory.
但在其他情况下,问题可能也是您从工厂返回null或未定义的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.