如何重用控制器而不添加类似的控制器? Angular1.5

时间:2016-09-21 09:42:40

标签: javascript angularjs controller

我在我的网络应用程序中完成了2个表(客户,项目)CRUD页面。

  1. CRUD客户表(customer.html)

  2. CRUD项目表(item.html)

  3. 我正在尝试制作一个新页面,类似于CRUD customer.html页面,但可以CRUD一个特定客户ID的项目。

    我要做的就是复制两个类似的页面。

    customer.html - > crudOneCustomersItems.html

    item.html - > item2.html

    然后我在app.js中创建了另一个1控制器:itemCtrl2,复制了类似的代码,但更改了数据源(项目属于一个客户的id)。

    crudOneCustomersItems.html item2.html 但我想也许我可以重用相同的控制器,我怎样才能在Angular1.5中实现它?

    我的演示插件:https://plnkr.co/edit/4hfNde9tLIIDzZRJAlhe

    app.controller('CustomerCtrl', function($scope) {
    
    // making data start 
    $scope.customer = {
      id:1,
      name:'jason',
      email:'pp@pp.cc'
    };
    $scope.customers = [];
    
    for(var i = 1 ; i < 20 ; i++){
        var temp = angular.copy($scope.customer);
        temp.id = i;
        temp.name = temp.name+i;
        temp.email = temp.name+i;
    
        $scope.customers.push(temp);
    }
    
    //making data end
    });
    
    app.controller('ItemCtrl', function($scope) {
    // making data start 
    $scope.item = {
      id:1,
      customerId: 2,
      name:'bomb',
      description:'it can explode'
    };
    $scope.items = [];
    
    for(var i = 1 ; i < 20 ; i++){
        var temp = angular.copy($scope.item);
        temp.id = i;
        if(i<10){
          temp.customerId =2;
        }
        else{
          temp.customerId =3;
        }
        temp.name = temp.name+i;
        temp.description = temp.name+i;
    
        $scope.items.push(temp);
    }
    
    
    //making data end
    });
    
    app.controller('ItemCtrl2', function($scope) {
    // getting selected data (customer id from querying parameter )
    });
    

    由于

0 个答案:

没有答案
相关问题