javascript - Accessing LinkedIn API from AngularJS - Stack Overflow

I am building an app with Angular.js accesing the LinkedIn API. What happens is that when I do a call t

I am building an app with Angular.js accesing the LinkedIn API. What happens is that when I do a call to the API, the model does not get refreshed inmediately, but after I do another change on the screen. For example, I have binded the API call to a button, but I have to press it twice for the screen to get the data refreshed. This is my controller:

    angbootApp.controller('AppCtrl', function AppCtrl($scope, $http) {
        $scope.getCommitData = function() {
            IN.API.Profile("me").fields(
                    [ "id", "firstName", "lastName", "pictureUrl",
                            "publicProfileUrl" ]).result(function(result) {
                //set the model
                $scope.jsondata = result.values[0];
            }).error(function(err) {
                $scope.error = err;
            });
        };
    });

And this is my button and a link with the content:

<div>
  <a href="{{jsondata.publicProfileUrl}}">{{jsondata.firstName}}</a>
  <form ng-submit="getCommitData()">
    <input type="submit" value="Get Data">
  </form>
</div>

EDIT: Explanation on how I did it here

I am building an app with Angular.js accesing the LinkedIn API. What happens is that when I do a call to the API, the model does not get refreshed inmediately, but after I do another change on the screen. For example, I have binded the API call to a button, but I have to press it twice for the screen to get the data refreshed. This is my controller:

    angbootApp.controller('AppCtrl', function AppCtrl($scope, $http) {
        $scope.getCommitData = function() {
            IN.API.Profile("me").fields(
                    [ "id", "firstName", "lastName", "pictureUrl",
                            "publicProfileUrl" ]).result(function(result) {
                //set the model
                $scope.jsondata = result.values[0];
            }).error(function(err) {
                $scope.error = err;
            });
        };
    });

And this is my button and a link with the content:

<div>
  <a href="{{jsondata.publicProfileUrl}}">{{jsondata.firstName}}</a>
  <form ng-submit="getCommitData()">
    <input type="submit" value="Get Data">
  </form>
</div>

EDIT: Explanation on how I did it here

Share Improve this question edited Sep 29, 2013 at 14:32 Eugenio Cuevas asked Jan 31, 2013 at 11:03 Eugenio CuevasEugenio Cuevas 11.1k3 gold badges31 silver badges51 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You need to call $scope.$apply when retrieving the results/error

angbootApp.controller('AppCtrl', function AppCtrl($scope, $http) {
    $scope.getCommitData = function() {
        IN.API.Profile("me").fields(
                [ "id", "firstName", "lastName", "pictureUrl",
                        "publicProfileUrl" ]).result(function(result) {
            //set the model
            $scope.$apply(function() {
                $scope.jsondata = result.values[0];
            });
        }).error(function(err) {
            $scope.$apply(function() {
                $scope.error = err;
            });
        });
    };
});

Anything outside angular world does not trigger automatic refresh of the views. See $apply

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745205164a4616559.html

相关推荐

  • javascript - Accessing LinkedIn API from AngularJS - Stack Overflow

    I am building an app with Angular.js accesing the LinkedIn API. What happens is that when I do a call t

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信