[Info]
what I am trying to achieve is to implement some custom angular directives that will encapsulate all the JS needed for them to work.
The directives are unaware of what they will be displaying and where to store any input value coming from the user. These information will come from the attributes of the directive.
My directives will use the parent scope and won't create their own one.
[Problem]
Since the directive does not know where in the $scope to map the ng-model and ng-bind, my approach is to read the directive's properties, identify what ng-model and ng-bing attributes shall be and set them to the corresponding elements. However this is not working.
I believe that its due to my lack of knowledge, so I am asking here - If my approach is OK?; Is it possible to set ngModel and ngBind in that manner?; What am I doing wrong?.
[My directive's code]
var directives = angular.module('test.directives', []);
directives.directive("labeledInput", function() {
return {
restrict: 'E',
scope: false,
template: "<div>" +
"<span class='label'></span>" +
"<input class='input' type='text'></input>" +
"</div>",
link: function(scope, element) {
var elementIdentifier = angular.element(element[0]).attr("idntfr");
var elementClass = angular.element(element[0]).attr("element-class");
var scopeValueName = angular.element(element[0]).attr("value-name");
var defaultValue = angular.element(element[0]).attr("default-value");
var elementLabel = angular.element(element[0]).attr("label");
scope[scopeValueName] = defaultValue;
scope[elementIdentifier] = elementLabel;
$(angular.element(element[0]).children()[0]).attr('id', elementIdentifier);
$(angular.element(element[0]).children()[0]).addClass(elementClass);
$(angular.element(element[0]).children().children()[1]).attr('ng-model', scopeValueName);
$(angular.element(element[0]).children().children()[0]).attr('ng-bind', elementIdentifier);
}
};
});
[Result]
As a result I see in the HTML page where the ng-model and ng-bind are binded at the right location, I have scope[scopeValueName] and scope[elementIdentifier] in the scope that Batarang provides, but I don't see them on my screen as values.
Have anyone ever solved a similar issue?
Thanks for your time!
[Edit] Sorry it seems my question wasn't understood I will add some details!
Here is the example HTML usage of my directive:
<labeled-input
idntfr='id001'
element-class='someClass'
value-name='person_name'
default-value='default'
label='Person Name:'
>
</labeled-input>
What I have in my browser after angular parse my directive and do it's jo is:
<div id="effect_dt" class="someClass">
<span class="label" ng-bind="id001"></span>
<input class="input" type="text" ng-model="person_name">
</div>
What I have in my controller scope is - $scope.id001 = "Person Name:" and $scope.person_name = default. However these values are not shown on the page at all.