2016-07-19 5 views
0

私はAngularJSには本当に新しく、別の配列、実際にはその1つの配列上に関数を再利用しようとすると難しかったです。私が間違っているところで私に会いましょう。ありがとう。Angularディレクティブの異なる配列で同じ機能を使用する

<body ng-controller="MainCtrl as mainCtrl"> 

<my-dates ng-repeat="item in mainCtrl.list"></my-dates> 

</body> 

app.directive('myDates', function() { 
    return { 
     // scope:{options:'&'}, 
     restrict:'E', 
     template:'<table class="myTable">' + 
     '<tr>'+ 
     '<th><input type="checkbox" ng-model="selectedAll" ng-click="mainCtrl.checkAll()"></th>'+ 
     '<th>Start Date:</th>'+ 
    '<th>End Date:</th>'+ 
    '<th>Hours:</th>'+ 
    '<th>Status:</th>'+ 
    '</tr>'+ 
    '<tr ng-repeat = "items in item">'+ 
     '<td><input type="checkbox" ng-model="items.selected"></td>'+ 
     '<td>{{items.start}}</td>'+ 
    '<td>{{items.end}}</td>'+ 
    '<td>{{items.hours}}</td>'+ 
    '<td>{{items.selected}}</td>'+ 
    '</tr>'+ 
    '</table>'+ 
    '<button class="myButton" ng-click="mainCtrl.add()">Add element</button>'+ 
    '<button class="myButton" ng-click="mainCtrl.remove()">Remove element</button>' 
    } 
}); 

app.controller('MainCtrl', function ($scope, $modal ,$log) { 
    var self = this; 
    self.list = [[{start:1,end:15,hours:7,selected: false},{start:2,end:14,hours:6,selected: false},{start:3,end:13,hours:5,selected: false},{start:1,end:15,hours:7,selected: false},{start:2,end:14,hours:6,selected: false},{start:3,end:13,hours:5,selected: false}], 
     [{start:1,end:15,hours:7,selected: false},{start:1,end:15,hours:7,selected: false}]]; 
    self.allSelected = false; 
    self.add = function() { 
     var modalInstance = $modal.open({ 
      template: '<div class="modal-header"><h1>Add element to array</h1></div>'+ 
     '<div class="modal-body">'+ 

      '<div>'+ 
      '<span>Enter Start Date:</span>'+ 
     '<input ng-model="object.start" type="text"/>'+ 
      '</div>'+ 

      '<div>'+ 
      '<span>Enter End Date:</span>'+ 
     '<input ng-model="object.end" type="text"/>'+ 
      '</div>'+ 

      '<div>'+ 
      '<span>Enter Work Hours:</span>'+ 
     '<input ng-model="object.hours" type="text"/>'+ 
      '</div>'+ 

      '</div>'+ 
      '<div class="modal-footer">'+ 
      '<button class="btn btn-primary" ng-click="ok()">OK</button>'+ 
      '<button class="btn btn-warning" type="button" ng-click="close()">Cancel</button>'+ 
      '</div>', 
      controller: 'PopupCont', 
      resolve: { 
       items: function() { 
        return $scope.items; 
       } 
      } 
     }); 
     modalInstance.result.then(function (object) { 
      self.list.push(object); 
     }, function() { 
      $log.info('Modal dismissed at: ' + new Date()); 
     }); 
    } 
    self.remove = function() { 
     console.log('tc'); 
     self.list = self.list.filter(
      function(item) { 
       return !item.selected 
      } 
     ); 
    } 
    self.checkAll = function(){ 
     if ($scope.selectedAll) { 
      $scope.selectedAll = true; 
     } else { 
      $scope.selectedAll = false; 
     } 
     angular.forEach(self.list, function(item) { 
      item.selected = $scope.selectedAll; 
     }); 
    } 
}); 
app.controller('PopupCont', ['$scope','$modalInstance',function ($scope, $modalInstance) { 
    $scope.object = {start:undefined,end:undefined,hours:undefined}; 
    $scope.ok = function() { 
     $modalInstance.close($scope.object); 
    }; 
    $scope.close = function() { 
     $modalInstance.dismiss('cancel'); 
    } 

}]); 
+0

質問にコードを入れてください – Joram

+0

done ............ –

答えて

0

私はあなたが次のことをやりたいと思う:

Plunkr

現時点ではあなたのコードは、変更のカップルのように、選択するためにリストするか分からないん

'<button class="myButton" ng-click="mainCtrl.add($index)">Add element</button>'+ 
    '<button class="myButton" ng-click="mainCtrl.remove($index)">Remove element</button> 

' 

次に、add:

self.add = function (index) { 

は、インデックスを参照してください。

modalInstance.result.then(function (object) { 
     console.log(object) 
     self.list[index].push(object); 
    }, function() { 
     $log.info('Modal dismissed at: ' + new Date()); 
    }); 

も削除に:

self.remove = function(index) { 
    console.log('tc'); 
    self.list[index] = self.list[index].filter(
     function(item) { 
      return !item.selected 
     } 
    ); 

が何をしたいということですか?

+0

私は私の問題を解決しました。ディレクティブのコントローラーのすべての機能を削除し、リストを自分のサブアイテムに変更しました。それは問題への良いアプローチですか? –

+0

それは同じように良い、私は角のスタイリストが何を言うだろうか分からない。そのコードが少なく、きちんとしていれば、それは良いです。それが他のリストやアプリケーションで再利用できる場合はさらにプラスになります。 – PeterS

関連する問題