比如有四个列表,布局几乎一样,只有细微的区别,Controller的代码自然也就非常类似了,比如只有请求参数不同。
initialize();
$scope.refreshHub[$scope.REFRESH_METHOD[$scope.nowIndex]] = function () {
$scope.model_request.pageNum = 1;
business_XW1.requestNewsList($scope.model_request).then(
function (okRs) {
$scope.list = okRs.response.parram;
setTimeout(function () {
$rootScope.loadMoreState = $scope.list.length >= $scope.model_request.pageSize;
},500);
},$scope.errorCallback
).always(function () {
$rootScope.$broadcast('scroll.refreshComplete');
});
};
$scope.loadHub[$scope.LOAD_METHOD[$scope.nowIndex]] = function () {
$scope.model_request.pageNum++;
business_XW1.requestNewsList($scope.model_request).then(
function (okRs) {
Array.prototype.push.apply($scope.list,okRs.response.parram);
$rootScope.loadMoreState = okRs.response.total > $scope.list.length;
},$scope.errorCallback
).always(function () {
$rootScope.$broadcast('scroll.infiniteScrollComplete');
});
};
// helper methods ~
function initialize() {
$scope.model_request = new xw_model.Corporation();
$scope.list = [];
}
比如这段代码,这四个列表的Controller中,只有model_request参数不同,那么我该如何才能复用这段代码呢?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
http://stackoverflow.com/questions/16539999/whats-the-recommended-way-to-extend-angularjs-controllers
把同样的逻辑封装成一个服务
如果要在controller中使用,通过下面的方式注入即可
放到service里,再调用啊,或者封装成一个指令啊
封装service
本人用的ui-router 直接在里面传值进去,对值进行判断,取得相应的方法获取数据