javascript - Angular View not updating after retrieving data from database -
i working on app retrieves data on of change of $routeparams. here's how begins:
function artistctrl($scope, $http, $location, datafactory, $routeparams){ $scope.artistname = $routeparams.artistname $scope.$watch('artistname', function(newvalue, oldvalue){ $scope.artistinfo = datafactory.getartist(newvalue) }) $scope.artistinfo = { artist_name: datafactory.artistinfo.artist_name, artist_genre: datafactory.artistinfo.artist_genre, artist_imageurl: datafactory.artistinfo.artist_imageurl, artist_bio: datafactory.artistinfo.artist_bio }; }
the callback $watch
here run. datafactory.getartist
retrieves newvalue
database being done successfully. done this:
datafactory.js
datafactory.getartist = function(artist){ return datafactory.checkdb(artist).then(function(dbdata){ if(dbdata.data != "no data"){ datafactory.artistinfo = dbdata.data[0] } return dbdata.data[0] }) } datafactory.artistinfo = "";
datafactory
factory created in file.
artistpage.html
<div class="container this"> <div class="row"> <div class="col-sm-6"> <div><h1>{{artistinfo.artist_name}}</h1></div> <div rate-yo id='stars' rating="myrating" class="col-sm-4"></div> <div id='reviews'>23 reviews</div> <div><h2>{{artistinfo.artist_genre}}</h2></div> <div><p>{{artistinfo.artist_bio}}</p></div> <div><button type="button" class="btn btn-primary" ng-click="somefunc()">submit review</button></div> </div> <div class="col-sm-6 reviews"> <div><img class="artistpageimage" src={{artistinfo.artist_imageurl}}></div> </div> </div> </div>
i don't understand why view isn't being updated. attempting update $scope.artistname assigning returned datafactory.getartist(newvalue)
, assigning new data datafactory.artistinfo
have read $apply, having hard time figuring out how apply in context. can help?
does getartist return promise or value. if it's promise try below:
$scope.$watch('artistname', function(newvalue, oldvalue){ datafactory.getartist(newvalue).then(function(value) { $scope.artistinfo = value; }) })
Comments
Post a Comment