I did one reduced snippet of my problem. Actually I'm doing an app using ionic, but it doesn't matter, the problem occurs in the plunker too.
I've also read this answer and updated some of my code, but the problem persists.
I have this controller with an object
:
app.controller('myCtrl', function($scope) {
$scope.object = {
boolean: true,
btn1: 'Do it be TRUE',
btn2: 'Do it be FALSE'
}
})
And I want to change its boolean
property inside this directive:
.directive('myDirective', function() {
return {
template: '<button class="btn btn-default">{{$obj.btn1}}</button>'
+ ' <button class="btn btn-default">{{$obj.btn2}}</button>',
restrict: 'A',
scope: { $obj: '=myDirective' },
link: function(scope, element, attrs) {
var listener = scope.$watch('$obj', function() {
if (scope.$obj) listener();
else return;
var buttons = element.find('button'),
btn1 = buttons.first(),
btn2 = buttons.last();
if(scope.$obj.boolean){
btn1.addClass('selected');
$('#Log').html('true');
}else {
btn2.addClass('selected');
$('#Log').html('false');
}
buttons.on('click', function(){
buttons.removeClass('selected');
$(this).addClass('selected');
var isTrue = this === btn1[0];
$('#Log').html(String(isTrue)); // works
scope.$obj.boolean = isTrue; // does not works
});
});
}
}
});
As you can see while running the plunker, the angular log does not update like the jQuery one, and I'm giving both the same value.
I am struggling with this by hours, any help would be really appreaciated, thanks for your time.