I am trying to bind a promise to a view. I don\'t know if you can do that directly, but that\'s what I\'m attempting to do. Any ideas what I am doing wrong?
Note:
returning a reference to the scope variable holding the list should suffice.
function addressValidationController($scope,$timeout) {
var regions = {
US: [{code: 'WI',name: 'Wisconsin'}, {code: 'MN',name: 'Minnesota'}],
CA: [{code: 'ON',name: 'Ontario'}]
};
$scope._regions = [];
$scope.getRegions = function () {
$timeout(function () {
var countryRegions = regions[$scope.countryCode];
console.log(countryRegions);
if(countryRegions === undefined) {
$scope._regions = []
} else {
$scope._regions = countryRegions
}
}, 1000);
return $scope._regions;
};
}
As of Angular 1.2, you can't use promises in templates directly anymore.
Instead, you need to put the result into $scope
inside then
, like you normally would—no magic.
As a temporary workaround to get the old behavior, you can call
$parseProvider.unwrapPromises(true)
but this feature will be removed later on, so don't depend on it.
WARNING: this answer was accurate when it was written, but as of 1.2 the Angular template engine does not handle promises transparently! -- @Malvolio
Yes the template engine (and expressions) handle promises transparently, but I would assign the promise to a scope property in the controller and not call everytime a function that returns a new promise (I think it's your problem, resolved promise is lost because a new promise is returned everytime).
JSFiddle: http://jsfiddle.net/YQwaf/36/
HTML:
<div ng:controller="addressValidationController">
Region Code <select ng:model="regionCode" ng:options="r.code as r.name for r in regions"/>
Country Code<select ng:model="countryCode"><option value="US">United States</option><option value="CA">Canada</option></select>
</div>
JS:
function addressValidationController($scope, $q, $timeout) {
var regions = {
US: [{
code: 'WI',
name: 'Wisconsin'},
{
code: 'MN',
name: 'Minnesota'}],
CA: [{
code: 'ON',
name: 'Ontario'}]
};
function getRegions(countryCode) {
console.log('getRegions: ' + countryCode);
var deferred = $q.defer();
$timeout(function() {
var countryRegions = regions[countryCode];
if (countryRegions === undefined) {
console.log('resolve empty');
deferred.resolve([]);
} else {
console.log('resolve');
deferred.resolve(countryRegions);
}
}, 1000);
return deferred.promise;
};
$scope.regions = [];
// Manage country changes:
$scope.$watch('countryCode', function(countryCode) {
if (angular.isDefined(countryCode)) {
$scope.regions = getRegions(countryCode);
}
else {
$scope.regions = [];
}
});
}
As of Angular 1.3 - $parseProvider.unwrapPromises(true)
will no longer work.
Instead, you should unwrap the promises directly:
myApiMethod().then(function(value){
$scope.item = value;
});
Note that promise unwrapping will still work with ngResource as usual.