我有一個模板與文本框在我的指令,點擊按鈕(ADD)我重複相同的指令10倍,所以10倍的文本框會來,但NG模型將對於每個文本框都保持不變,並且我需要進行動態化處理,以便模板ng-model的每次重複都變得不同。 問題是我無法創建文本框的動態ng模型來區分輸入的值,以便我可以在我的控制器中訪問它。如何使文本框的模型動態化。如何在我的模板中動態模型angularjs
App.directive("configDirectives", function($compile) {
return {
restrict: 'EA',
link: function(scope, element, $attr) {
console.log('Scope in directive : ' + scope);
scope.add = function() {
console.log("Inside directive value of satCount", satCount++);
$newDirective = angular.element('<add-config></add-config>');
element.append($newDirective);
$compile($newDirective)(scope);
console.log('Scope in directive : ' + scope);
}
}
}).directive("addConfig", function() {
return {
restrict: 'AE',
template: '<div>{{scope.satCount}}' +
'<input type="text" ng-model="x"/>' +
'</div>',
link: function(scope, element, attribute) {
scope.remove = function() {
element.remove();
}
}
});
<!-- Controller -->
(function() {
var self = null;
var ConfigRuleClass = Class.extend({
init: function($scope, configService) {
self = this;
self.$scope = $scope;
},
save: function() {
console.log("values from parent configuration---");
console.log("config1---", self.lstConfigs.name);
console.log("Dynamic Filed Data" + self.dynamicConfigs);
}
});
App.controller("ConfigRuleCntrl", ['$scope', 'configService', ConfigRuleClass]);
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div id="xx" data-ng-controller="ConfigRuleCntrl as y">
<input type="text" ng-model="y.x" />
<button data-ng-click="add()">Add</button>
<br>
<button data-ng-click="y.save()">SAVE</button>
<config-directives></config-directives>
</div>
你能爲你的問題創造plnkr? – dhavalcengg
你如何重複文本框? – dfsq
應該是我們在這裏失蹤的ng-repeat?點擊按鈕(ADD) – n00b