Hi Sander,
I was reading your small plunk and I can't help but notice it is way
to complicated. I wouldn't call it the AngularJS way. The rule #1
would be: keep is simple, but it's not. What I would call the
AngularJS way would be to make the model/controller tandem (not the
directive's controller) to be the source of the truth. This is the
"view" which should depend on model/controller, not the other way
around.
In your case, one reads the model/view:
$scope.$watch('seSmartBg.expose', function(string) {
console.log('Exposed by directive:' + string);
// here I will use the API to set an initial color!
// I do it in this watch, because then I'm sure the directive
// is up and running.
$scope.seSmartBg.setBG('#fafafa');
});
and the first question is: what ON EARTH is this 'seSmartBg' thing
coming from. I know that sometimes we have to code against the tragedy
called: "who know where does this thing in $scope come from", but I
think one should avoid it at all cost and use it only when everything
else happens to be more complicated.
In your example, the ultimately simple implementation would look like this:
controller:
$scope.model = {currentColor: 'red'}; //just the default
directive:
<div ng-class="{{model.currentColor}}">
<button ng-click="model.currentColor = 'red'>red</button>
<button ng-click="model.currentColor = 'green'>green</button>
<button ng-click="model.currentColor = 'blue'>blue</button>
</div>
Isn't it as simple as one can get? You can see what's going on in
fraction of second: model is the source of truth, view looks at the
model and trigger controller's actions (in this case it's simplified,
there are no true controller methods declared explicitly, but it's
just a matter of AngularJS awesomeness that we can sometimes get rid
of boilerplate).
Regards,
Witold Szczerba
2014-06-03 16:12 GMT+02:00 Sander Elias <[email protected]>:
> Hi All,
>
> As an sample to his question, I created this small plunk to show a way on
> how this can be done. It is a sort of discussion starter.
> For myself, I think this is a very angular way to expose selected parts to
> the view and to the scopes inside the directive.
> also child-directives can pick up the controller and use the exposed API.
>
> Did I miss some serious drawbacks in this approach?
>
> Regards
> Sander
>
> --
> 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.