내에서 사용하고있는 고립 스코프 지시문이 있습니다.이 지시문은 해당 템플릿의 컨트롤러에서 배열을 반복합니다. 다음과 같이 템플릿은 다음과 같이ng-repeat 내의 Angularjs 지시문이 컨트롤러 데이터 외부에서 업데이트되지 않습니다
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="bootstrap.min.css" />
<script src="angular.min.js"></script>
<script src="script1.js"></script>
</head>
<body ng-app="AddNewTest" ng-controller="AddNewController">
<div class="items" ng-repeat="row in rows">
<add-new-row data="row" index="$index"></add-new-row>
</div>
</body>
</html>
이 지침이 정의 :
angular.module('AddNewTest', []).
directive('addNewRow', function ($timeout) {
return {
controller: 'AddNewController',
link: function (scope, element, attribute) {
element.on('keyup', function(event){
if(scope.index + 1 == scope.rows.length) {
console.log('keyup happening');
$timeout(function() {
scope.rows.push({ value: '' });
scope.$apply();
});
}
})
},
restrict: 'E',
replace: true,
scope: {
index: '='
},
template: '<div class="add-new"><input type="text" placeholder="{{index}}" ng-model="value" /></div>'
}
}).
controller('AddNewController', function ($scope, $timeout) {
$scope.rows = [
{ value: '' }
];
});
그러나 NG-반복 새로운 데이터가 추가 렌더링하지 않아도 새 행을 추가하고 $apply()
을 수행 한 후. 도와주세요. 행
귀하는 ** 지침 ** 범위 **를 보유하고 있습니다. 지시어에'rows' 배열을 전달하십시오. 이 범위와 같이 : { index : '=', rows : "=" }'.'AddNewController' 컨트롤러에 행을 만들지 마십시오. '$ scope.rows = [ {value : ''} ]'을 제거하십시오. –
어떻게 작동 시키시겠습니까? 각 값에 대한 입력? 어레이에 밀어 넣기 위해 사용되는 입력은 단 하나입니까? 어떤 결과를 기대합니까? – gyc
@gyc 마지막 행에 keyup 이벤트에 새 행을 추가하고 싶습니다. – jsbisht