I am trying to add form elements dynamically using JS and will need a directive. I am able to add form elements but when I have ng-options or ng-repeat it does not get compiled. I have an example directive I am using for demo.
http://plnkr.co/edit/JOzTWB6tuyilCJ8Rj37Q
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller("fCtrl",function($scope){
$scope.xx = ['x','c','y','z','a'];
});
app.directive('datanType', function ($compile) {
var testTemplate1 = '<h1 ng-repeat="x in xx">Test</h1>';
var testTemplate2 = '<h1>Test2</h1>';
var testTemplate3 = '<h1>Test3</h1>';
var getTemplate = function(contentType){
var template = '';
switch(contentType){
case 'test1':
template = testTemplate1;
break;
case 'test2':
template = testTemplate2;
break;
case 'test3':
template = testTemplate3;
break;
}
return template;
};
var linker = function(scope, element, attrs){
element.html(getTemplate(attrs.content));
$compile(element.contents())(scope);
};
return {
restrict: "E",
replace: true,
link: linker,
scope: {
content:'=',
con:'@'
}
};
});
</script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body ng-controller="fCtrl">
<p>Result:</p>
<datan-type content="test1" con="{{xx}}"></datan-type>
</body>
</html>