javascript - angular resolve returns function instead of value -
i trying refactor code out of controller , route's 'resolve' , having trouble getting correct return value resolve
. think problem either due misunderstanding of js closures or $q library in angular.
in routing:
.when('/countries/:id', { templateurl: 'country/country.html', controller: 'countryctrl', resolve: { activecountry: ['countrydata', '$q', '$route', function(countrydata, $q, $route) { var defer = $q.defer(); countrydata($route.current.params.id).then(function(data){ console.log(data); defer.resolve(data); }); return defer.promise; }] } })
the console.log
in block returns correct json object api call. however, when passed controller, controller returns function countrydata
service:
.controller('countryctrl', [ 'countrydata', '$routeparams', '$scope', function (activecountry, $routeparams, $scope) { var countryid = $routeparams.id; console.log(activecountry); $scope.mapid = $routeparams.id.tolowercase(); $scope.country = activecountry; } ]);
the value of routing console.log
is: object {countryname: "spain", currencycode: "eur", fipscode: "sp", countrycode: "es", isonumeric: "724"…}
but controller console log function countrydata
service:
function (countryid) { var defer = $q.defer(); $http.get(countries_path + '&country=' + countryid + api_auth, { cache: true }).success(function (data) { defer.resolve(data.geonames[0]); }); return defer.promise; }
why object routing not being passed controller correctly?
also, why need use $q
in router when built countrydata
service? without using $q
in routing, returns function rather function's return value.
you put 'countrydata' , activecountry controller arguments. not match up.
it should 'countrydata' , countrydata
controller('countryctrl', [ 'countrydata', '$routeparams', '$scope', function (countrydata, $routeparams, $scope) { var countryid = $routeparams.id; console.log(activecountry); $scope.mapid = $routeparams.id.tolowercase(); $scope.country = activecountry; } ]);
Comments
Post a Comment