1

I have a controller, with $scope.colorHex value

For example I have the directive colorpickerTooltip and in the template I call another directive: <colorpicker ng-model="colorHex"></colorpicker>

then in the second directive (colorpicker) I set up controller's value:

scope.colorHex = '#cecece';

and then in the second directive I call method scope.doIt(), but something is going wrong:

my $scope value is not updated! But why, maybe because of my 2-level directive?

How to set up the controller variable value?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
brabertaser19
  • 5,678
  • 16
  • 78
  • 184

2 Answers2

0

Yes this is the problem of two level directive scope. Please read https://github.com/angular/angular.js/wiki/Understanding-Scopes

Define the colorHex in your controller as an object like:

$scope.dataStore = {};
$scope.dataStore.colorHex = '#cecece';

Then modify at first directive as:

<colorpicker ng-model="dataStore.colorHex"></colorpicker>

And then later use it in your second directive to update in doIt method:

$scope.doIt = function() {
    $scope.dataStore.colorHex = '#fff';
}
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
0

Second directive copies the value from the first one and creates a new object in the second scope. You can prevent it via using a objective structure such as

$scope.color= {hex=undefined};

and change it in the inside scope

$scopescope.color.hex = '#cecece';

it will prevent this stuation

Utku Apaydin
  • 162
  • 1
  • 4