This is a hard topic for newcomers. I am going to make a resume here to make you understand it.
When you're doing something like:
<my-directive>
<button ng-click="doSomething()">Do Something</button>
</my-directive>
Your first thought is that everything inside <my-directive>
, in this case the <button>
is part of the directive. That is not true.
What is inside, again, the <button>
is called transcluded html
. That transcluded html
has a new scope which have no access to the isolated scope of the directive, in fact they are siblings.
In concrete, there is a controller's scope and from there there are a isolated scope and a new scope for the transcluded html.
So since the transcluded html can't access the isolated scope, that ng-click
won't fire at all.
There are workarounds for that.
So in this case, I repeat, doSomething
exists on the isolated scope and that button is looking into the new scope of the transcluded html
and on its parent (the controller's scope) and it doesn't exist.
The solution that sh3nan1gans gave to you is working, but I wouldn't do that personally.
How does it work? Since it creates a two-way binding with the controller's scope, it creates the new function on the isolated and then it is also created on the parent. Then the button will find that function in that parent scope and then run it.
If you want more information, including the workaround, check here