2

我有一个返回 JSONP 有效负载的资源:

app.factory('JsonService', function($resource) {
    return $resource('//123.456.7.890\\:8888/user/:id/:model',
    {'page': ':page', 'limit' : ':limit', 'jsonp_callback' : 'JSON_CALLBACK'}, 
    {})
});

我的控制器调用它:

app.controller("followersCtrl", function ($scope, $dialog, JsonService) {
  ...
  $scope.user_id = $.globals.authed_user_id;
  $scope.model = "followers"
  $scope.loadJSONP = function (getPage) {
      JsonService.get({
          id: $scope.user_id,
          page: getPage,
          limit: $scope.pagination_limit,
          model: $scope.model
      },

      function (data) {
          $scope.followers = data;
          $scope.noOfPages = data.page_count;
          $scope.currentPage = data.page;
      });
  };
  $scope.loadJSONP(1);
  $scope.pageChanged = function (page) {
      $scope.callbackPage = page;
      $scope.loadJSONP(page);
  };
});

然后我循环返回/存储在的数据$scope.followers

<div ng-controller="followersCtrl">
    ...
    // Code related to the modal omitted
    ...

<div ng-repeat="user in followers.data">
         <img class="img img-polaroid" ng-src="{{user.image_url}}"/>
     <p>{{ user.username }}</p>
         <button class="btn btn-disabled" ng-show="user.is_mutual">
          You Are following this user
         </button>
         <button class="btn btn-primary" ng-show="!user.is_mutual">
          You Aren\'t following
         </button>
     <p>{{user.total_followers}}</p>

</div>
    ...
    // Code related to the modal omitted
    ...
</div>

这一切都按预期工作,但对于我的生活,我无法弄清楚如何让“你没有关注”按钮更改{{post.total_followers}}点击时相应表达式中反映的值。理想情况下,我想执行 aPUT来实际执行后续操作,但我决定从小处着手,只更新值,但无济于事。

任何帮助表示赞赏。谢谢你。

4

1 回答 1

1

ng-click 是您要查找的内容吗?

HTML:

<button class="btn btn-primary" ng-show="!user.is_mutual" ng-click="follow(user)">
  You Aren\'t following
</button>

JS:

app.controller("followersCtrl", 
  function ($scope, $dialog, JsonService) {
    $scope.follow = function(user){
      // code for posting to server-side follow action
      // and updating the total_followers
      // $scope.user.total_followers++; // this should be ideally incremented on success callback
    }
  }
);
于 2013-03-12T19:46:03.903 回答