I am trying to create a generic directive in angular, for example, take this html:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Testing directives</title>
<script src="../scripts/angular.js"></script>
<script type="text/javascript">
angular.module('ngDirTest', [
//controllers
])
</script>
<script src="../scripts/populate.js"></script>
<script src="../Directive/GenericDirective.js"></script>
</head>
<body ng-app="ngDirTest">
<div ng-controller="PopulateCtrl">
<div generic-directive ng-model="nameone"></div>
<div generic-directive ng-model="nametwo"></div>
<div generic-directive ng-model="namethree"></div>
<div generic-directive ng-model="namefour"></div>
<div generic-directive ng-model="namefive"></div>
<div generic-directive ng-model="namesix"></div>
</div>
</body>
</html>
and take this controller:
angular.module("ngDirTest").controller('PopulateCtrl', ['$scope', function($scope) {
//
}]);
and take this directive:
angular.module('ngDirTest').directive('genericDirective', [ function() {
return {
restrict: 'A',
scope: {
fn: "&"
},
template: '<input type="text" ng-model=name placeholder="type a name" />',
link: function(scope) {
scope.$watch('name', function(val) {
//
})
}
}
}]);
the idea is to have a template that can has different placeholders for the different text input tags. Is there a good way to do this?
I was thinking about getting the model from the input text being watched and then do something with it, but that won't help me set the placeholders and initialise the the input text fields.