anglarJs前端控制器的继承

继承就是将相关内容进行抽离,将公共部分进行分离,然后被其他模块继承就可以实现多用

例如抽离分页部分:

 //品牌控制层 
app.controller('baseController' ,function($scope){    
    
    //重新加载列表 数据
    $scope.reloadList=function(){
        //切换页码  
        $scope.search( $scope.paginationConf.currentPage, $scope.paginationConf.itemsPerPage);           
    }
    
    //分页控件配置 
    $scope.paginationConf = {
         currentPage: 1,
         totalItems: 10,
         itemsPerPage: 10,
         perPageOptions: [10, 20, 30, 40, 50],
         onChange: function(){
             $scope.reloadList();//重新加载
          }
    }; 
    
    $scope.selectIds=[];//选中的ID集合 

    //更新复选
    $scope.updateSelection = function($event, id) {        
        if($event.target.checked){//如果是被选中,则增加到数组
            $scope.selectIds.push( id);            
        }else{
            var idx = $scope.selectIds.indexOf(id);
            $scope.selectIds.splice(idx, 1);//删除 
        }
    }
    
    
    $scope.jsonToString=function(jsonString,key){
        
        var json= JSON.parse(jsonString);
        var value="";
        
        for(var i=0;i<json.length;i++){
            if(i>0){
                value+=",";
            }            
            value +=json[i][key];            
        }
                
        return value;
    }
    
});    

这下面就是继承代码,是一种伪继承,通过scope来传递,就是让basecontoller的scope跟当前scope相等,可以进行参数传递

$controller('baseController',{$scope:$scope});//继承

要想执行还必须要注入服务:

app.controller('brandController' ,function($scope,$controller   ,brandService){

猜你喜欢

转载自www.cnblogs.com/xiufengchen/p/10369242.html