0

my html looks as

 <div class="row" align="center" ng-if="searchCtrl.valid">
    <div class="col-lg-12"><button type="button" class="btn btn-default">Authorize to Instagram</button><br/></div>
   </div>

the js is

app.controller('AdminController', ['$scope', function($scope){
        this.valid = true;

        //see if ig login exists once login is performed using google
        gapi.client.instagramApi.validateIgLogin().execute(function(resp) {
             if(resp && resp.hasOwnProperty('error')) {
                 // error
                 alert(resp.error.message);
              }else{
                  //successful response
                  console.log(resp);
                  this.valid = resp.valid;
              }
             });
        }

my service is coded to return false always. and i am expecting this to refresh ui and hide the button. its not working thoguh

3
  • 1
    I'm not seeing any $scope update/assignments in your example Commented May 5, 2016 at 9:33
  • $scope.$apply(); ? after last stmt in resp ? i added that but no luck Commented May 5, 2016 at 9:37
  • be careful using this inside callback functions. You can never be sure that it's going to be what you expect. Why not move this.valid to $scope.valid. You'll probably have to update your binding to ng-if="valid" as well. Commented May 5, 2016 at 9:50

2 Answers 2

1

You're having two problems.

This first one is that this in your gapi callback doesn't refers to the controller anymore, so you may want to keep a reference to your controller.

The second one is that this gapi call is asynchronous and not part of the Angular api, so Angular will not be aware of any change that is made within its callback until the next digest cycle. In order to force a digest cycle, so can use $scope.$apply.

Here is a solution with both fixes:

app.controller('AdminController', ['$scope', function($scope) {
  var thisController = this;

  this.valid = true;

  //see if ig login exists once login is performed using google
  gapi.client.instagramApi.validateIgLogin().execute(function(resp) {
    if(resp && resp.hasOwnProperty('error')) {
      // error
      alert(resp.error.message);
    } else {
      //successful response
      console.log(resp);
      $scope.$apply(function() {
        thisController.valid = resp.valid;
      });

    }
  });
}

Note that if you accept ES6 syntax, you can keep reference to the original this by using arrow functions:

app.controller('AdminController', ['$scope', function($scope) {
  this.valid = true;

  //see if ig login exists once login is performed using google
  gapi.client.instagramApi.validateIgLogin().execute((resp) => {
    if(resp && resp.hasOwnProperty('error')) {
      // error
      alert(resp.error.message);
    } else {
      //successful response
      console.log(resp);
      $scope.$apply(() => {
        this.valid = resp.valid;
      });

    }
  });
}
Sign up to request clarification or add additional context in comments.

Comments

1

use the $appy service to bind data through the instergram

app.controller('AdminController', ['$scope', function($scope){
        this.valid = true;

        //see if ig login exists once login is performed using google
        gapi.client.instagramApi.validateIgLogin().execute(function(resp) {
             if(resp && resp.hasOwnProperty('error')) {
                 // error
                 alert(resp.error.message);
              }else{
                  //successful response
                  console.log(resp);
                 $scope.$apply(function(){ this.valid = resp.valid; })

              }
             });
        }

2 Comments

care to explain why ?
because, If you use external API (jQuery, pure javacript ..etc) whatever. it's not the angular component. Angular framework is different. if you bind data to angular object from non angular object you have to inform that to angular. $apply , $digest are the methods provide by the angular to do that... otherwise angular not recognize changes

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.