Angular Directive Follow / Unfollow按钮

时间:2015-02-15 05:54:09

标签: javascript angularjs angularjs-directive

我正在尝试使用角度指令按钮来关注和取消关注联盟ID 每个请求$ http.put都没问题但.then方法控制台显示错误而方法有问题

这里是代码

app.factory('FollowedLeagues', ['appConfig', '$http', '$q', function(appConfig, $http, $q){

    var FollowedLeagues = {};

    FollowedLeagues.follow = function (token, leagueID) {
      $http.put(appConfig.apiUrl + 'user/follow-league?token=' + token +'&league_id='+ leagueID +'&status=true' )
        .then(function(response){

          if (typeof response.data === 'object') {
            return response.data;
          } else {
            // invalid response
            return $q.reject(response.data);
          }

        },
        function(response) {
          // something went wrong
          return $q.reject(response.data);
        });
    };

    FollowedLeagues.unfollow = function (token, leagueID) {
      $http.put(appConfig.apiUrl + 'user/follow-league?token=' + token +'&league_id='+ leagueID +'&status=false' )
        .then(function(response){

          if (typeof response.data === 'object') {
            return response.data;
          } else {
            // invalid response
            return $q.reject(response.data);
          }

        },
        function(response) {
          // something went wrong
          return $q.reject(response.data);
        });
    };


    return FollowedLeagues;

  }]);
app.directive('fbFollowBtn', ['$rootScope', '$compile', 'FollowedLeagues', function ($rootScope, $compile, FollowedLeagues) {



    var getLeagueID = function(leagueID, followed){

      for(var i=0; i< followed.length; i++) {
        var fLeagues = followed[i]._id;
        if (fLeagues == leagueID) {
          return fLeagues;
        }

      }

    };//End-function.


    return {
      restrict: 'A',

      link:function(scope, element, attrs){

        scope.followed = $rootScope.meData.followedLeagues;
        scope.leagueid = attrs.leagueid;

        var follow_btn = null;
        var unfollow_btn = null;

        var createFollowBtn = function () {

          follow_btn = angular.element('<a href="javascript:void(0)" ng-disabled="submitting">Follow</a>');

          $compile(follow_btn)(scope);
          element.append(follow_btn);

          follow_btn.bind('click', function(e){
            scope.submitting = true;
            FollowedLeagues.follow($rootScope.userToKen, scope.leagueid)
              .then(function(data){
                scope.submitting = false;
                follow_btn.remove();
                createUnfollowBtn();

                console.log('followed Leagues Done :-) ', data);
              });
           // scope.$apply();

          });

        };


        var createUnfollowBtn = function () {
          unfollow_btn = angular.element('<a href="javascript:void(0)" ng-disabled="submitting">Unfollow</a>');
          $compile(unfollow_btn)(scope);
          element.append(unfollow_btn);

          unfollow_btn.bind('click', function (e) {
            scope.submitting = true;

            FollowedLeagues.unfollow($rootScope.userToKen, scope.leagueid)
              .then(function(data){
                scope.submitting = false;
                unfollow_btn.remove();
                createFollowBtn();

                console.log('followed Leagues Done :-) ', data);
              });
           // scope.$apply();
          });



        };



        scope.$watch('leagueid', function (val) {

          var leag = getLeagueID(scope.leagueid, scope.followed);


          if(typeof(leag) == 'undefined'){

            createFollowBtn();

          } else if(typeof(leag) !== 'undefined'){
            createUnfollowBtn();
          }//end if


        }, true);




      }
    };
  }]);

enter image description here

1 个答案:

答案 0 :(得分:2)

您必须在服务功能中返回$ http.put函数。例如Followedleagues.follow函数:

FollowedLeagues.follow = function (token, leagueID) {
  return $http.put(appConfig.apiUrl + 'user/follow-league?token=' + token +'&league_id='+ leagueID +'&status=true' )
    .then(function(response){

      if (typeof response.data === 'object') {
        return response.data;
      } else {
        // invalid response
        return $q.reject(response.data);
      }

    },
    function(response) {
      // something went wrong
      return $q.reject(response.data);
    });
};