Binding issue in AngularJS

61 views Asked by At

I am trying to bind from a http get request. The http get is returning true or false. I have tested the get and it is returning properly. When I run the code below, it shows the alert(1111) properly also. However, when I'm trying to change the button text, nothing appears! I have tried everything that I know to do. Any advice would be helpful.

Post.js

myApp.controller('FollowController', ['$scope', '$http', function($scope, $http) {

    var status = "";

        $http.get('/Home/CheckFollower?idToFollow=' + profileId + '&followerId=' + currentUserId).
            success(function(data) {
                //check if it is a follower

                if (data) {
                    // Not following - Show unfollow
                    alert("1111");
                    $scope.statusMessage = data;


                } else {
                    //Following - show Follow

                    $scope.statusMessage = data;
                }

            })
            .error(function(data, status) {
                console.log(data);
            });

}]);

Html

   <span style="float: right" ng-controller="FollowController as follow">
                        <button type=" button" class="btn btn-success" onclick="location.href='@Url.Action("Follow", "Home", new { idToFollow = ViewBag.ProfileId, followerId = User.Identity.GetUserId() })'">
                            {{ follow.statusMessage }}</button>

                        </span>
1

There are 1 answers

3
Pankaj Parkar On BEST ANSWER

You should bind the variables to this instead of $scope as you are using controllerAs approach

Controller

myApp.controller('FollowController', ['$scope', '$http',
    function($scope, $http) {
        var status = "";
        var follow = this;
        $http.get('/Home/CheckFollower?idToFollow=' + profileId + '&followerId=' + currentUserId).
        success(function(data) {
          //check if it is a follower
          if (data) {
            // Not following - Show unfollow
            alert("1111");
            follow.statusMessage = data;
          } else {
            //Following - show Follow
            follow.statusMessage = data;
          }
        })
        .error(function(data, status) {
          console.log(data);
        });
    }
]);