user2919261 user2919261 - 3 months ago 10
AngularJS Question

dynamic load function for ng-click with evel

You will find plunder of this here : plunker

UI is like this : http://i.stack.imgur.com/7H1b3.png

I have ng-repeat with which I want to create ng-click in directive template.
Template name as mainTemplate.html and its code is below code:

<form class="class">
<p>Inside directive</p>

<div ng-repeat='mainJson in mainJsonData'>
<div class="{{mainJson.divClass}}">
<input ng-model="mainJson[mainJson.inputNgModel]" type="{{mainJson.inputType}}" class="{{mainJson.inputClass}}" placeholder="{{mainJson.placeHolder}}" maxlength='{{mainJson.inputMaxLength}}' value="{{mainJson.value}}" ng-click="mainJson[mainJson.ngClick]()"/>
</div>
</div>
</div>
</form>


Directive:

add.directive('testData', function() {
return {
templateUrl: 'template/mainTemplate.html',
controller: function($scope, $http) {
$http.get('json/mainUi.json').success(function(response) {
$scope.mainJsonData = response;
});
}
};
});


And json is with which i am trying to create directive :

[
{
"divClass":"form-group",
"inputType":"text",
"inputClass":"form-control",
"inputNgModel":"mobile",
"inputMaxLength":"10",
"placeHolder":"mobile"
},
{
"divClass":"form-group",
"inputType":"button",
"inputClass":"btn btn-success",
"ngClick":"addData",
"value":"Save",
"ngClickData":"name, city, mobile"
}
]


and I am looking for ng-click as :

ng-click="addData(name, city, mobile)


which should work with ng-model as ng-model also get created same from

Answer

You can split the string in a array.

Like the example: https://plnkr.co/edit/6RHVKKhWQC4r3JsBAPEJ?p=preview

I put hard coded what you have, insted the name|city|mobile your is mainJson[mainJson.inputNgModel].

UPDATE

Its works, but idk if its good do what you are doing, the maintence will be very dificulty.

The working version based on yours: https://plnkr.co/edit/opDKuIYkDmmR7Nk4xZeP?p=preview