外部コントローラデータを更新しない私はそのテンプレートのコントローラからアレイ上に反復されるIは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-repeatが新しいデータが追加レンダリングされていなくても、新しい行を追加し、$apply()
をした後。助けてください。
あなたはディレクティブで** **分離株のスコープを持っています。ディレクティブに 'rows'の配列を渡します。この '範囲のよう:{ 度: '='、行: "=" }' 'AddNewController'コントローラの行を作成.Don't。 '' $ scope.rows = [ { '' 値}]を取り除きます。 –
これはどのように機能しますか?各値の入力?配列にプッシュするために使用される入力は1つだけですか?あなたはどんな結果を期待していますか?私は最後の行でからkeyupイベントに新しい行を追加したいと思い@gyc – gyc
。 – jsbisht