I'm getting started with Angular.js directives, so I'm dynamically generating DOM with custom directives.
The simplified version of my directive is:
angular.module('app', [])
.controller('Controller', ['$scope',
function($scope) {
$scope.name = "André Pena";
$scope.email = "andrerpena@gmail.com"
}
])
.directive('gText', function() {
return {
restrict: 'E',
link: function(scope, element, attrs) {
//input
var input = angular.element("<input/>");
input.attr("type", "text");
input.addClass("form-control");
input.attr("placeholder", attrs.placeholder);
input.attr("name", attrs.property);
element.append(input);
}
};
});
And a simple use of this directive is:
<g-text label="E-mail" property="email" placeholder="Enter some e-mail"></g-text>
As you can see, I'm creating an input
tag dynamically using an Angular.js element
. I want to bind the value
of this element with the property specified in the property
attribute. In this case, I want the value of the input to be the email
scope property (andrerpena@gmail.com).
How to achieve that?