Javascript Angular Typeerror无法读取未定义的属性get
我正在阅读其他stackoverflow文章和更改代码,但我似乎无法理解这一点Javascript Angular Typeerror无法读取未定义的属性get,javascript,angularjs,Javascript,Angularjs,我正在阅读其他stackoverflow文章和更改代码,但我似乎无法理解这一点 angular.module('app') .controller('DefaultCtrl', ['$scope', function ($scope) { }]) 工厂是问题所在 .factory('autoCompleteDataService', [function ($scope, $http) { return { getSource: function($s
angular.module('app')
.controller('DefaultCtrl', ['$scope', function ($scope) { }])
工厂是问题所在
.factory('autoCompleteDataService', [function ($scope, $http) {
return {
getSource: function($scope) {
$http.get("GetPersonInfoFromRegister/T")
.success(function(data) {
$scope.countries = data;
});
}
这个指令很好,我试图修改工厂,从静态返回中从ajax调用返回数据
.directive('autoComplete', function (autoCompleteDataService) {
return {
restrict: 'A',
link: function (scope, elem, attr, ctrl) {
// elem is a jquery lite object if jquery is not present,
// but with jquery and jquery ui, it will be a full jquery object.
debugger;
elem.autocomplete({
source: autoCompleteDataService.getSource(), //from your service
minLength: 1
});
}
};
});
}
}])
当我使用一个示例并刚刚返回时,工厂正在工作
return ["john", "bill", "charlie", "robert", "alban", "oscar", "marie", "celine", "brad", "drew", "rebecca", "michel", "francis", "jean", "paul", "pierre", "nicolas", "alfred", "gerard", "louis", "albert", "edouard", "benoit", "guillaume", "nicolas", "joseph"];
$http
是异步的,并返回一个承诺。在您的情况下,它甚至不返回任何内容,而是将$scope.countries
设置为一个值。您的指令似乎希望设置的是返回值,而不是$scope
上的属性。为了从web api调用返回数据,我需要更改什么?$http.get(“GetPersonInfo fromRegister/T”)它在对象处抛出未定义的get错误。getSource抛出错误的不是$http.get
,而是您定义的getSource
函数没有返回任何内容。