I have a template with text box in my directive,on click of button(ADD) i am repeating the same directive 10 times so 10 times text box will come but ng-model will remain same for each text box and this i need to make dynamic so that on each repeat of template ng-model becomes different. Problem is I am not able to create dynamic ng-model for text box to distinguish between the values entered so that I can access it in my controller.How to make model of text box dynamic.
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>