This is a two part question:
Part 1
What's the difference between binding to an event, say click
using the ngClick
attribute, vs using the element.bind('click')
? For instance, I can have
HTML
<div ng-click="doStuff()">
JS
// Link function of a directive
link: function(scope, element, attrs)
{
scope.doStuff = function() { // doing stuff //}
}
OR, I could ignore the ng-click
inside my <div>
and straight forward jump to the directive:
link: function(scope, element, attrs)
{
element.bind('click', function()
{
// doing stuff
});
}
Part 2
Now, clearly there is a difference in what I can only assume is the $digest
cycle. I have the following function that runs upon ng-click
to create a new entry, and this entry is then gets a class added to it:
$('.cell').removeClass('selected');
scope.shift.createTask(scope.grid);
scope.schedule.modified();
// Wait until the DOM is digested before finding the last entry
$timeout(function(){
var elem = element.closest('.shift').find('.task:last-child');
activateTask(elem);
},0);
The above code works fine if I use the ng-click
method. But when I use the element.bind('click')
method, the last entry with the $timeout
doesn't add the class. It always seems to be one behind (i.e. the first task
added and nothing happens. The second task
added, and the first task gets the class)