0

これが私のシナリオです。

見る:

<div data-ng-controller="MyModuleController">
   <div ng-repeat="a in range() track by $index" >
      <ty-project idx="$index+1" ng-model="projects[$index]" ></ty-project>
   </div>
</div>

コントローラ:

$scope.projects= [];

$scope.range = function() {
    // return some random number - it does not really matter for the purpose
};

ty-project は単なるディレクティブです

angular.module('mymodule').directive('tyProject', [
     function() {
         return {
            templateUrl: 'modules/mymodule/directives/typrojectTemplate.html',
             restrict: 'E',
             scope: {
                   idx: '='
             },
             link: function postLink(scope, element, attrs) {
             }
       };
    }
]); 

typrojectTemplate は、次のように 2 つの入力を持つ単純なテンプレートです。

  <div>
      <label>Project_{{idx}} name</label>
      <input type="text" name="project.name" ng-model="project.name"/>
  </div>
  <div >
     <label >Project_{{idx}} total </label>
     <input type="number" name="project.total" ng-model="project.total" />
 </div>

これは私のコントローラーです

angular.module('mymodule').controller('MyModuleController', ['$scope', 
    function($scope) {

       $scope.projects: [] ;

        $scope.save = function() {

            console.log(projects);
        };

        $scope.range = function() {
            var n= 2;// todo: return random number..
            return new Array(n);
        };
    }
]);

そのため、範囲メソッドが 2 を返す場合、各プロジェクトには name 属性と total 属性を持つ 2 つのプロジェクト オブジェクトがあります。

プロジェクトをコントローラーにバインドするにはどうすればよいですか?

ありがとう。

4

1 に答える 1

1

コントローラーからディレクティブにスコープを渡す必要があります。ディレクティブは、このスコープをテンプレートに渡します。

ディレクティブでこれを行うことができます:

scope:{
       project: '=ngModel'//will pass it to the Template.
       idx: '=' //
}

コントローラーをビューに割り当てたかどうかはわかりません。

于 2014-12-17T21:27:01.757 に答える