Angularjs 无法将数据从工厂传递到控制器
我有一个工厂的代码:Angularjs 无法将数据从工厂传递到控制器,angularjs,Angularjs,我有一个工厂的代码: angular.module("mainApp", ["kendo.directives"]) .factory("getFormats", function() { var searchStr="list&John&1&1".substr(1); //Here I see the searchStr return { isType: function(){
angular.module("mainApp", ["kendo.directives"])
.factory("getFormats", function() {
var searchStr="list&John&1&1".substr(1);
//Here I see the searchStr
return {
isType: function(){
searchStr.split('&')[0];
//Here I see the searchStr
},
username: function(){
searchStr.split('&')[1];
},
dateFormatIndex: function(){
searchStr.split('&')[2];
},
languageIndex: function(){
searchStr.split('&')[3];
}
}
})
.controller("ValidationListCtrl", function( $scope,getFormats) {
var isType=getFormats.isType(); //Here I see the undefined
var username=getFormats.username();//Here I see the undefined
var dateFormatIndex=getFormats.dateFormatIndex();//Here I see the undefined
var languageIndex=getFormats.languageIndex();//Here I see the undefined
}
但我得到的所有变量在控制器中都是未定义的。我还检查了工厂,当我在控制器中得到它,看到它与空对象
Object {}dateFormatIndex: (){arguments: nullcaller: nulllength: 0name: ""prototype: dateFormatIndex__proto__: Empty() {}<function scope>isType: (){languageIndex: (){username: (){__proto__: Object
Object{}dateFormatIndex:(){arguments:nullcaller:nulllength:0name:“prototype:dateFormatIndex____;u:Empty()
我遗漏了什么?忘记使用工厂退货方法,现在应该可以了:)
忘记在工厂中使用return方法,现在应该可以了:)
您没有在工厂内的任何方法中返回值。请执行以下操作:
angular.module("mainApp", ["kendo.directives"])
.factory("getFormats", function() {
var searchStr="list&John&1&1".substr(1);
//Here I see the searchStr
return {
isType: function(){
return searchStr.split('&')[0];
//Here I see the searchStr
},
username: function(){
return searchStr.split('&')[1];
},
dateFormatIndex: function(){
return searchStr.split('&')[2];
},
languageIndex: function(){
return searchStr.split('&')[3];
}
}
});
这应该可以解决问题。工厂中的任何方法都不会返回值。请执行以下操作:
angular.module("mainApp", ["kendo.directives"])
.factory("getFormats", function() {
var searchStr="list&John&1&1".substr(1);
//Here I see the searchStr
return {
isType: function(){
return searchStr.split('&')[0];
//Here I see the searchStr
},
username: function(){
return searchStr.split('&')[1];
},
dateFormatIndex: function(){
return searchStr.split('&')[2];
},
languageIndex: function(){
return searchStr.split('&')[3];
}
}
});
另一种方法是使用模块化/显示模块模式,这有助于封装和代码可读性,如下所示
angular.module("mainApp", ["kendo.directives"])
.factory("getFormats", function() {
var _searchStr="list&John&1&1".substr(1),
_isType = function(){
return _searchStr.split('&')[0];
},
_username = function(){
return _searchStr.split('&')[1];
}
.....
return {
isType :_isType ,
username:_username
.....
}
})
因此,在您的控制器或其他服务中,您可以将其作为
getFormats.username(//arguments if required)
username.isType(//arguments if required)
另一种方法是您可以使用模块化/显示模块模式,这有助于封装和代码可读性,如下所示
angular.module("mainApp", ["kendo.directives"])
.factory("getFormats", function() {
var _searchStr="list&John&1&1".substr(1),
_isType = function(){
return _searchStr.split('&')[0];
},
_username = function(){
return _searchStr.split('&')[1];
}
.....
return {
isType :_isType ,
username:_username
.....
}
})
因此,在您的控制器或其他服务中,您可以将其作为
getFormats.username(//arguments if required)
username.isType(//arguments if required)
谢谢你的精彩回答!谢谢你的精彩回答!谢谢你的精彩回答!请结束问题:)(并让我获得一些分数;))谢谢你的精彩回答!请结束问题:)(并让我获得一些分数;))谢谢你的精彩回答!@TuviaKhusid没问题,Tuvia,很乐意帮助:):)谢谢你的精彩回答!@TuviaKhusid没问题,Tuvia,很乐意帮助:):)