0
이 내 시나리오,AngularJS와 - 동적 크기 배열 NG 모델
보기 :
<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
};
타이 프로젝트가 단지 지시
입니다angular.module('mymodule').directive('tyProject', [
function() {
return {
templateUrl: 'modules/mymodule/directives/typrojectTemplate.html',
restrict: 'E',
scope: {
idx: '='
},
link: function postLink(scope, element, attrs) {
}
};
}
]);
typrojectTemplate은 간단합니다. 다음과 같이 두 입력이있는 템플릿 :
<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>
이 2 있을 것 2 개 프로젝트 각 객체 프로젝트 이름과 전체 속성을 가지고 있으므로 경우 범위 방법의 대가로 내 컨트롤러
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);
};
}
]);
입니다.
어떻게 프로젝트를 컨트롤러에 바인딩 할 수 있습니까?
감사합니다.
제안 해 주셔서 감사합니다. 그러나 도움이되지 않았습니다. 질문에 대한 마지막 업데이트를 참조하십시오. –
컨트롤러를보기에 추가 했습니까? 컨트롤러 코드 pls – AngularLover
을 확실하게 표시 할 수 있습니까? 내 질문을 업데이트했습니다. –