JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<script type="text/ng-template" id="a.html">
a.html
</script>
<script type="text/ng-template" id="b.html">
{{data}}
<div ng-if="data.values">
<div ng-repeat="itm in data.values">
<input name='{{itm.name}}' id='{{itm.name}}' ng-model="itm.name" type='text'></input>
</div>
</div>
</script>
<div ng-controller="TestController">
<directive data="data"></directive>
</div>
JavaScript
var myApp = angular.module('myApp', [])
.controller('TestController', ['$scope', function ($scope) {
$scope.data = [{ type: "a", values: [{name:"aa"}, {name: "bb"}]},
{ type: "b", values: [{name:"aa"}, {name: "bb"}]}]
}]);
myApp.directive('directive', function () {
return {
scope: {
data: "=",
},
restrict: 'E',
controller: function ($scope, $element, $attrs) {
$scope.random = function(){
$scope.model = $scope.data[Math.floor(Math.random()*$scope.data.length)];
console.log($scope.model)
};
},
template: '<div><button ng-click="random()">Random</button><sub-directive data="model"></sub-directive></div>'
};
});
myApp.directive('subDirective', function ($templateCache, $compile) {
return {
scope: {
data: "=",
},
restrict: 'E',
link: function (scope, element, attrs) {
scope.$watch('data', function (newVal, oldVal) {
if (newVal) {
element.html($templateCache.get(newVal.type + '.html'));
$compile(element.contents())(scope);
}
});
}
};
});