AngularJS docs say:
$q promises are recognized by the templating engine in angular, which means that in templates you can treat promises attached to a scope as if they were the resulting values.
So could someone please explain the reason this fiddle not working? It's not possible to change text field value. But assigning promises that $http service returns to a scope field works like a charm.
Controller:
function MyController($scope, $q, $timeout) {
this.getItem = function () {
var deferred = $q.defer();
deferred.resolve({
title: 'Some title'
});
return deferred.promise;
};
$scope.item = this.getItem();
}
Html:
<input type="text" ng-model="item.title">
$scope.item = $http({method: 'post', url: '/find/my/item/'}) .then(function (response) { return response.item; });
Another example that uses $resource approach can be found in this tutorial. Starting from the line:Notice how in PhoneListCtrl we replaced ... with $scope.phones = Phone.query();
– Vespasian.success(function(){}).then(function(r){if (r.data["my-result"])return r.data["my-result"];});
– Liatrice