Hi,
you problem is very common. Your directive has it's own scope. Yes, it
does inherit from the scope of your controller, but it is not the same
one.
Check the JavaScript language reference on object inheritance. In your
case the solutions are:
- do not make your directive creating new scope (add scope:false),
- add an intermediate object, so your child scope of directive can
access it, like this:
controller:
$scope.something = {myClass: "class1";};
directive's bind:
$scope.something.myClass = "class2";
This way you will not get in a trap of covering a parent's object
(scope in your case) property in a child.
Regards,
Witold Szczerba
2014-05-02 13:39 GMT+02:00 Enes Lisovac <[email protected]>:
> Hello everyone...Can anybody explain me what I'm doing wrong here...
> Let's say that I'm having something like this:
>
> var app = angular.module("app", []);
>
> app.controller("controller", function ($scope) {
> $scope.myClass = "class1";
> });
>
> app.directive("myDirective", function () {
> return {
> restrict: "AEC",
> replace: true,
> template: "<div ng-class='myClass'></div>",
> link: function ($scope, elem, attrs) {
> elem.bind("click", function () {
> $scope.myClass = "class2";
> });
> }
> }
> });
>
> As far as I can understand when I click on the proper <div> it should change
> the value of parent scope variable "myClass" to "class2" value. But on the
> UI this change is not reflecting. Should I create maybe isolated scope or
> something like that.
> Is it possible to change values of the parent scope from the directive ?
> Please disregard all potential miss typos in the code...
>
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.