So I was just testing some stuff and I was writing a directive like this:
HTML
<div ng-app="app">
<my-directive>
hi {{name}}
</my-directive>
</div>
JS
angular.module("app", [])
.directive("myDirective", function () {
return {
scope:true,
restrict: "E",
link: function (scope, elem, attr) {
scope.name="yourname";
elem.onclick = function () { console.log("change name");}
}
}
})
But the onclick function never fires. And I know this is not how I should properly write in Angular, but it was just something I did on the fly. However, this works:
angular.module("app", [])
.directive("myDirective", function () {
return {
scope:true,
restrict: "E",
link: function (scope, elem, attr) {
scope.name="yourname";
elem.on("click", function () {console.log("change name")});
}
}
})
And here a fiddle: fiddle
What is going on here? Is it outside the Angular world and should I use scope.applyor something to get it to work. I am just curious why vanilla javascript is not ok in this case. But this is also a good lesson to keep myself focused on thinking the Angular way.
Maybe someone that can explain what is going on? TIA
restrict: "E"but your markup is using it as anattribute, orrestrict: "A"-- Edit: your question is right, your fiddle is wrong. Sorry for the confusion