im confused about 2 way data binding in angular. Look at the code!
the var bah can access parent object $scope.something but when i click the button, the value in controller changed to false but not in directive. whats wrong? is that a bug?
how to solved this? thanks to help me, hope u please write an example to or ref links
HTML
<div ng-controller="myController">
show me something {{ something }} <br>
<button ng-click="toggleSomething"><button>
<!-- this is a canvas -->
<my-directive></my-directive>
</div>
JS
angular.module('fooBar',[]).controller('myController', ['$scope', function($scope) {
// this is the something
$scope.something = true;
$scope.toggleSomething = function(){
if($scope.something) {
$scope.something = false
} else {
$scope.something = true
}
}
}]).directive('myDirective', function(){
return {
template: '<canvas width="500px" height="500px"></canvas>',
link: function(scope, el, attr) {
//how to access that 'something'
var bah = scope.$parent.something;
}
};
});
UPDATE Really thanks to you all. especially to u @immirza
im so sorry i cant reply u one by one.
it just add $parent
//how to access that 'something'
var bah = scope.$parent.something
somethingis not changing indirective?console.log(scope.$parent.something);is prints one time (that should be true) if you put it in directive link function isn't it? so how do you manage to check the value after the click in directive to confirm that value is not changing?