ngModel is locked into the isolate scope when used on a component
When ngModel directive is used on an element that represents a component (implemented via a directive with isolate scope), ngModel is locked into this isolate scope and in order to get out and make ngModel useful the ngModel expression has to be prefixed with $parent.
so instead of:
{{my.model}}
<my-component ng-model="my.model"></my-component>
one has to write:
{{my.model}}
<my-component ng-model="$parent.my.model"></my-component>
this is non-intuitive and might be hard to debug for developer unfamiliar with what's going on.
the solution must however take into account that it's possible to use ngModel within the template of the isolated component, in which case we must not leak the model outside of the component.
btw the foodme app that we created as a tutorial app suffers from this issue: https://github.com/IgorMinar/foodme/blob/master/app/views/restaurants.html#L13
Another drawback: the use-site has to write $parent.my.model, so if you want to provide a re-usable component, the implementation of it leaks to all users of the component :-(
@IgorMinar I just ran into this issue myself trying to create an angular component. Isolate scope seems to be by-far the most idiomatic looking way of writing directives but nearly 100% of the time the isolate-ness has been too agressive and made it impractical to use. Really frustrating. It seems like the problem is a design one, not a bug or anything.
Any advice regarding future plans for isolate?
FYI, when I ran into this problem, I used < ng-model="portal.myVar" portal="parentScopeVar" > Where this was my isolate scope definition: { ... portal: "=" ... }
Which then made changes to (in the parent scope): parentScopeVar.myVar Conceptually, it opens a "portal" to the parent scope through which ngModel can work.
Not a particularly elegant workaround, but it worked nicely, at least.
I just ran into this again. Hard. Gah... this issue stands in the way of a cow-path I'm afraid. Its pretty critical to creating angular components.
@mhevery @IgorMinar Could $setViewValue support an optional second param for scope?
controller.$setViewValue(foo, scope.$parent);
There must be a better way than ^ though.
i have a suggestion. I believe all that must be done is to provide a way to the user instruct a bi-directional bind without specifying the local property name, but the name of attribute that contains the local property name.
Look, this is going to fail:
<my-component ng-model="my.model"></my-component>
scope: {
ngModel: '=ngModel'
}
Currently, we are saying that when the user changes $scope. ngModel we'll reflect to $scope.$parent.my.model. All we got to do is to provide an attribute binding, something like this:
scope: {
'=ngModel': '=ngModel'
}
So instead of binding local.ngModel to parent.my.model, we bind local.my.model (as ng-model="my.model") to parent.my.model. This would solve the problem and let it transparent to any directive or the outside user, requiring no refactoring at directives at all. Also, any other directive that hits the same problem can be easily circumvented with this approach.
I can try to implement it if Angular team approve this approach.
in the input.js we have:
var ngModelGet = $parse($attr.ngModel), ngModelSet = ngModelGet.assign;
maybe explicity modifing the ngModelCtrl by asking for them in the link of the component so you can make something like ngModel.$setContext(scope) //or ngModel.$setContext($scope.parent)
auto binding at the parser everything the model is setting/getting
This is the workaround I currently use on my directive: ... scope: { id: '@', // any custom attribute outterModel: '=ngModel' // get ahold of the model }, ... // keep model in both scopes in sync: scope.$watch('outterModel', function(val, old) { scope.$eval(attrs.ngModel + ' = outterModel'); }); scope.$watch(attrs.ngModel, function(val, old) { scope.outterModel = val; });
I got the original idea form here: http://stackoverflow.com/questions/15269737/why-is-ngmodel-setviewvalue-not-working-from
@dobladez 's workaround has been useful for me too.
Please checkout this solution
https://github.com/angular/angular.js/pull/2904
As is often the case with Angular, the easiest work-around is simple element nesting.
<div ng-model="">
<my-component />
</div>
.directive('myComponent', function () {
return {
scope: {},
require: '^ngModel'
/* ... */
};
});
@hon2a yeah! it is but not it enough elegant
Yup from : http://stackoverflow.com/a/15272359/390330
scope: {
model: '=ngModel'
}
And then in your linking function keep both in sync:
// Bring in changes from outside:
scope.$watch('model', function() {
scope.$eval(attrs.ngModel + ' = model');
});
// Send out changes from inside:
scope.$watch(attrs.ngModel, function(val) {
scope.model = val;
});
The solution by @inzurrekt is nice But my number 1 preference would be to have it more natively supported: https://github.com/angular/angular.js/issues/1924#issuecomment-20410735
I'd still rather to have a native solution, that would make this sync transparent to the developer, making it eligible to further optimizations out of the box.
So I still think an additional signature would solve this problem, like here https://github.com/angular/angular.js/issues/1924#issuecomment-16140066
Basically, allowing the user to bind to the same value as the attribute. This have a bonus of working for any other attribute we might need.
@caiotoon I agree, that would be the ultimate solution. But I would prefer a syntax more consistent with what's already there i.e. @,=,&. Perhaps ^
<my-component ng-model="my.model"></my-component>
scope: {
model: '^ngModel'
}
From an implementation logic point of view it would mean that if you (within the directive) do:
scope.model = 123;
that would set the following for you, automatically, using $parse internally:
scope.$parent.my.model = 123;
Additionally with ^ you would still get the behaviour you get from =
After a lot of thought. The simplest solution seems to be the one offered by @inzurrekt here: https://github.com/angular/angular.js/pull/2904
Basically have a $setContext function on ngModel:
ngModel.$setContext(scope.$parent);
The reason why this function is required is $scope is available via closure (and not a member of ngModel) so its not easily changable otherwise.
Simple modification in AngularJS
Based on @inzurrekt idea it can be solved with a simple $setScope function in ngModelController:
https://github.com/basarat/foodme/blob/master/app/lib/angular/angular.js#L11667
this.$setScope = function(context) {
$scope = context;
}
You can see the pull request: https://github.com/angular/angular.js/pull/3148
Given that above is present in AngularJS
You can see that the directive no longer needs $parent in the html : https://github.com/basarat/foodme/blob/master/app/views/restaurants.html#L13
<fm-rating ng-model="filter.rating"></fm-rating>
All you need to do in your directive definition is set the scope for ngModel: https://github.com/basarat/foodme/blob/master/app/js/directives/fmRating.js#L15
// Move scope to parent for isloate scope ngModels
ngModel.$setScope(scope.$parent);
And you can see the application still works : https://github.com/basarat/foodme
Since that pull request would probably (and validly since it only fixed ngModel) get rejected, here are some other ideas. Solution 2 is more work but backward compatible. Solution 1 is simpler but backward incompatible (and I feel a better solution overall)
Solution 1
If the directive has an isolate scope and requires a controller
scope :{},
require: '?ngModel',
At the point where a required controller gets instantiated and a scope gets injected. The injector should pass scope.$parent automatically to (in this case) the ngModelController (or whatever else is required).
This would be different behaviour from what is there right now but I feel the current behaviour is actually not functional. However if you want the current behaviour to continue (e.g. to not break people that used $parent. in the expression) then having a generic method to support the a scope change in the injected directive controllers would be required (my pull request only fixed ngModel).
Which brings me to:
Solution 2
Building on previous suggestion of mine:
scope :{},
require: '=ngModel',
= (or any other letter you want, maybe &) would mean use the scope of the parent when instantiating the ngModelController. This would depend upon scope (don't use parent if not an isolate scope).
Would the angular team agree on using something like this?
Maybe the solution (just for NgModel) is more simple. I thinked it a long time and finally can say there is no reason to use an ngModel for the isolated properties of an input, but to use for connect an "input Module" with a data model... in other words when you have an ngModel on the same node of an isolated scope directive we can assume you want to evaluate on the parent
Looks like this issue has been open for half a year. Any ideas for viable solutions in the near term?