如何通过单击按钮初始化angularJS工厂

时间:2018-04-13 19:25:42

标签: angularjs

我有一个工厂从API加载数据。代码看起来像

var query = from csp in db.ChatSessionPersons
            join cm in db.ChatMessages on csp.ChatSessionId equals cm.ChatSessionId into cmj
            select new {
                ChatSessionId = csp.ChatSessionId, 
                UnreadCount = (from cm in cmj where cm.Id > csp.LastReadChatMessageId select cm).Count()
            };

我将其初始化为.factory('iScroll', function($http) { var iScroll = function(pre_url){ this.items = []; this.busy = false; this.page = 1; this.pre_url = pre_url; } iScroll.prototype.nextPage = function(){ if(this.busy) return; this.busy = true; var url = this.pre_url + 'page='+this.page; $http.get(url).then(function(response){ console.log(response.data); for (var i = 0; i < response.data.length; i++){ this.items.push(response.data[i]); console.log("loading..."); } this.page++; this.busy = false; }.bind(this)); }; return iScroll; })

我的问题是,如何通过点击按钮致电工厂?

1 个答案:

答案 0 :(得分:1)

在模板中,您将使用ngClick指令触发操作:

<button type="button" ng-click="loadUsersAction()">Load Users</button>

然后在您的控制器中,您定义该功能:

$scope.loadUsersAction = function() {
  $scope.load_users = new iScroll('/api/web/branches/users/' +$stateParams.id+'?');
};