angularjs - Promise in the template engine does not seem to work -
i trying set $scope variable promise , use scope variable in template engine not seem work.
in controller:
$scope.getuser = function() { // returning promise // need in function angular calls function // more once promise may change if new user logs in, // i.e. myself new return userservice.getmyself(); }
service:
var userdeferred = $q.defer(); // other code here resolves promise. // have verified promise resolved. service.getmyself = function () { return userdeferred.promise; };
in html:
<button> {{getuser().username}} </button>
i read angular docs:
$q promises recognized templating engine in angular, means in templates can treat promises attached scope if resulting values.
but imagine misinterpreted , using wrong. have function return because user change (promise changes) , want template engine pick fact has changed regards promise (ie created new promise user changed , want template see that.
edit:
here plunker show trying at: http://plnkr.co/edit/6j9pwy?p=preview
i not sure not working (you not give enough information) here sample code seems work:
http://embed.plnkr.co/ve5kv8m1ubvheatptjfk/preview
javascript
angular.module('mymodule', []) .factory('userservice', function($q, $timeout) { var getmyself = function() { var userdeferred = $q.defer(); $timeout(function() { userdeferred.resolve({username: 'root'}); }, 2000); return userdeferred.promise; }; return { getmyself: getmyself }; }) .controller('helloctrl', function($scope, userservice) { $scope.user = userservice.getmyself(); });
html
<body ng-app="mymodule" ng-controller="helloctrl"> <h1>user:</h1> {{user.username}} </body>
Comments
Post a Comment