javascript - Getting data to $scope fails in angularjs -
i'm trying pass data $scope.post doing way, doesn't want work imagine do. no errors in console , code inside postcontroller.
var postcontroller = function ($scope, $location, $routeparams, $http, post, user) { $scope.post = {}; $scope.getbyid = function (id) { return $http.get('/api/post/' + id).success(function (data) { $scope.post = data; }); } $scope.getbyid($routeparams.id); console.log($scope.post); } here's routing too:
when('/post/:id', { controller: postcontroller, templateurl: 'post.html' }). edit, whole postcontroller added.
getbyid async call, it's not failing, you're not waiting! use .then return promise , assignment in callback:
$scope.getbyid = function (id) { return $http.get('/api/post/' + id).then(function (result) { return result.data }); } $scope.getbyid($routeparams.id).then(function(data) { $scope.post = data; });
Comments
Post a Comment