0

I am currently working on an app that retrieves data on the of change of $routeParams. So here's how it 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 for $watch here is run. dataFactory.getArtist retrieves newValue from my database which is being done successfully. That is done like 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 is a factory I created in another 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 a 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 my view isn't being updated. I am attempting to update $scope.artistName by assigning the returned dataFactory.getArtist(newValue) and also by assigning the new data to dataFactory.artistInfo I have read about $apply, but I am having a hard time figuring out how to apply it in this context. Can anyone help?

2 Answers 2

2

Does getArtist return a promise or a value. If it's a promise try something like the below:

$scope.$watch('artistName', function(newValue, oldValue){
  dataFactory.getArtist(newValue).then(function(value) {
    $scope.artistInfo = value;
  })
})
Sign up to request clarification or add additional context in comments.

Comments

2

I think the problem is that dataFactory.getArtist(newValue) is returning a promise, which you're assigning directly to artistInfo. Try replacing it with:

dataFactory.getArtist(newValue).then(function (info) {
    $scope.artistInfo = info;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.