I am trying to create a directive & attach ng-click
with the template.
So I expecting that if I click on the template it will log the statement from scope.scrollElem
function, which is not happening.
I am able to create the directive but it is not responding to click.
Design approch
If this directive is attached to this DOM element, it will insert a div
element before & after this DOM. This inserted div
have an image (want to attach ng-click to this image) which will respond to an event.
Directive module
//rest of code
return {
restrict: 'EAC',
controllerAs: 'vm',
controller: _controller,
link: _link
}
};
Link function
function _link(scope, elem, attrs) {
console.log("Method Executing:Navigator._link");
var params = scope.$eval(attrs.navparams);
//Separate function is used to create template as
//the template is dependent on params value
scope.createNavigatorTemplate = _createNavigatorTemplate(scope, elem, params);
scope.scrollElem = function() {
console.log("abc");
}
}
Creating template
function _createNavigatorTemplate(scope, elem, params, $compile) {
params.forEach(function(item) {
var _template = angular.element('<div class="' + item.class + '"></div>');
var _img = angular.element('<img>').attr({
src: item.image,
alt: item.imageAlt,
'ng-click':"scrollElem()" // attaching ng-click here
});
//appending template with element
//_img.bind('ng-click',scope._scrollElem) //tried this but not working
_appendTemplate(elem, item.dir, _template);
})
}
I checked this SO question but still not able to resolve the issue.