angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

ngModel is locked into the isolate scope when used on a component

Open IgorMinar opened this issue 13 years ago • 39 comments

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.

IgorMinar avatar Jan 30 '13 21:01 IgorMinar

@pheuter The short term solution I copied out : https://github.com/angular/angular.js/pull/3148 was horrible. Sure it makes ng-model work but for ng-change / others you still need to do $parent, if you use them on the same element as the directive.

My new potential recommendation:

Basically a new option (perhaps called singleIsolate:true) that would mean do not leak the isolate scope to other directives on the same html element yet create a new one for this directive.

The location to modify would be around: https://github.com/angular/angular.js/blob/master/src/ng/compile.js?source=cc#L934-L1017

basarat avatar Aug 13 '13 06:08 basarat

@basarat, one of the premises is that we have only one scope per element, acting as a common medium for sharing messages, and thus, only one scope shared among all directives of a specific element. This change would also demand rethinking and maybe changing and redesigning scope() method and related features.

As we probably will need to address mixed cases (isolated and mixed properties), maybe the best solution is still to have a way of defining the two-way binding in a per property way.

caiotoon avatar Aug 19 '13 18:08 caiotoon

I've been bugged by this issue for the longest time. It's really annoying and it basically means you cannot easily write standalone components in angular.

I think a possible solution is to simply create a shared scope for an element if there is at least one directive that needs its own scope (normal or isolate). Then, for each directive requiring an isolate scope an isolate scope is created whose $parent would be the shared scope.

If backwards compatibility is not an issue, then the easiest would be to always created a shared and an isolate scope for a directive that requiring a scope. This way, each directive would have a way to store private scope data and public scope data.

Would something like this not work or is there something fundamentally wrong with angular that prevents this issue from being addressed? I had really hoped this would be fixed for 1.2

spamdaemon avatar Sep 05 '13 13:09 spamdaemon

This is not only an issue with ngModel, but with several other directives as well. Like ngShow, which has a similar thread going: https://github.com/angular/angular.js/issues/2500

In my mind, bindings placed on an element with an isolate scope should just bind to the parent scope. That's it. Why else would you be putting ngModel or ngShow on the element? No one wants them to bind to the isolate scope.

The real question to ask is if there a compelling reason NOT to have it this way?

kevinsbennett avatar Sep 10 '13 21:09 kevinsbennett

In my mind, bindings placed on an element with an isolate scope should just bind to the parent scope.

:+1:

This does seem only logical. I'm with @kevinsbennett with this one so far. I can't come up with a scenario where that is not the case (then again, I didn't really give it that much of a thought). Any one? God knows the shameful hacks I've made to components with isolated scopes work together with other directives :/

diegovilar avatar Sep 10 '13 22:09 diegovilar

I was just confused by this issue. I don't have anything to contribute re a solution, but am watching :)

xaviershay avatar Sep 11 '13 01:09 xaviershay

Until there is no proper solution for that problem, I also use the workaround 'dobladez' described above.

// 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 synchronization with the two $watch-functions works well, but if you use an array-value as the model, it breaks that concept.

<my-directive ng-model="myArray[0]"></my-directive>

When I do this, I get an excpetion when the first $watch-function evalues the expression:

TypeError: Cannot set property '0' of undefined
    at Function.extend.assign (http://localhost:9000/basTestClient/app/components/angular/angular.js:6518:59)
    at http://localhost:9000/basTestClient/app/components/angular/angular.js:6368:21
    at Object.$get.Scope.$eval (http://localhost:9000/basTestClient/app/components/angular/angular.js:8218:28)
    at Object.fn (http://localhost:9000/basCore/services/util/basDirectiveUtil.js:20:23)
    at Object.$get.Scope.$digest (http://localhost:9000/basTestClient/app/components/angular/angular.js:8097:27)
    at Object.$get.Scope.$apply (http://localhost:9000/basTestClient/app/components/angular/angular.js:8304:24)
    at done (http://localhost:9000/basTestClient/app/components/angular/angular.js:9357:20)

Does anybody have a solution for that problem?

anagelcg avatar Oct 08 '13 10:10 anagelcg

Would a directive for ng-Model-type binding on isolate scopes be an appropriate solution? That's what I've decided to do for the time being. It required me to roll my own ngModelController, which was a bit of a pain and a lot of duplication of the internal Angular implementation, but it in effect binds the ng-model to the parent scope, which appropriately connects as I expect it to.

I called it ngModelIsolate and an example of it can be found here: http://plnkr.co/edit/7kzEM1Dj2llK7o1SzVQM

-MikeMac

On Tue, Oct 8, 2013 at 5:05 AM, anagelcg [email protected] wrote:

Until there is no proper solution for that problem, I also use the workaround 'dobladez' described above.

// 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 synchronization with the two $watch-functions works well, but if you use an array-value as the model, it breaks that concept.

When I do this, I get an excpetion when the first $watch-function evalues the expression:

TypeError: Cannot set property '0' of undefined at Function.extend.assign (http://localhost:9000/basTestClient/app/components/angular/angular.js:6518:59) at http://localhost:9000/basTestClient/app/components/angular/angular.js:6368:21 at Object.$get.Scope.$eval (http://localhost:9000/basTestClient/app/components/angular/angular.js:8218:28) at Object.fn (http://localhost:9000/basCore/services/util/basDirectiveUtil.js:20:23) at Object.$get.Scope.$digest (http://localhost:9000/basTestClient/app/components/angular/angular.js:8097:27) at Object.$get.Scope.$apply (http://localhost:9000/basTestClient/app/components/angular/angular.js:8304:24) at done (http://localhost:9000/basTestClient/app/components/angular/angular.js:9357:20)

Does anybody have a solution for that problem?

— Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/1924#issuecomment-25877990 .

MikeMcElroy avatar Oct 08 '13 17:10 MikeMcElroy

I created an $isolator service as a temporary fix : https://github.com/basarat/demo-angularjs-easeljs/blob/gh-pages/typescript/ka/isolator.ts what this allows you to do is use an isloate scope within a directive but not have this isolate scope leaked to other directives on the same element, so you don't need to use $parent hack on other directives (e.g ng-model ng-show) on the same element.

See sample app : http://basarat.github.io/demo-angularjs-easeljs/ And the sample usage : https://github.com/basarat/demo-angularjs-easeljs/blob/gh-pages/typescript/directives/annotateimageDirective.ts#L16-L22

basarat avatar Oct 11 '13 01:10 basarat

Sometimes, a widget directive needs to work with minimum effort of the developers who use it. Also it needs to have its own isolated scope to maintain its state.

for example:

<input type="text" onchange="verify()" ng-model="username" name="username">

change to

<input type="text" onchange="verify()" ng-model="username" name="username" delay="500">

to add the delayed execution behavior.

It would be too much to modify the ng-model or add additional attribute for the obj.attr workaround.

here is the workaround I came up with:

mod.directive('delay', function() {
    return {
        restrict: 'A',
        scope: { name1: "="},
        transclude: true, 
        compile:function (element, attrs, transclude) {
            if (attrs.ngModel) {
                attrs.$set("name1", attrs.ngModel, false);
                attrs.$set('ngModel', 'name1', false);
            }
        }
    };
  })

sample test here: http://jsfiddle.net/pccXS/

wanjungao avatar Oct 12 '13 04:10 wanjungao

this creates some weird things: http://plnkr.co/edit/7bad6WxZ8H18XJTAqOK3

eddiemonge avatar Oct 16 '13 00:10 eddiemonge

Awesome guys Thank you! Time for real components now ;)

revolunet avatar Nov 08 '13 07:11 revolunet

This is really good news. I do have one question: what is the relationship between an isolate directive and a directive with shared scope on the same element?

spamdaemon avatar Nov 08 '13 14:11 spamdaemon

Does this mean it's going into 1.2 stable when it rolls out? Pretty significant breaking change if you developed a workaround for it.

-MikeMac

On Fri, Nov 8, 2013 at 8:16 AM, spamdaemon [email protected] wrote:

This is really good news. I do have one question: what is the relationship between an isolate directive and a directive with shared scope on the same element?

— Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/1924#issuecomment-28065161 .

MikeMcElroy avatar Nov 08 '13 14:11 MikeMcElroy

@MikeMcElroy 1.2 is out and this commit is a part of the changelog : https://github.com/angular/angular.js/blob/master/CHANGELOG.md

I thinks it's awesome :)

basarat avatar Nov 08 '13 23:11 basarat

@spamdaemon same as the relationship between isolate scope and the scope of a parent element.

basarat avatar Nov 08 '13 23:11 basarat

thanks @pheuter for suggesting $parent thing! but I still have problem using $parent in typeaheads that I have in my templates. ng-change works fine on all inputs checkboxes but not on typeahead!

shilan avatar Oct 01 '15 13:10 shilan

@basarat

ngModel.$setScope(scope.$parent);

Is this even made to final commit or has been removed as of 1.5.6? I stumbled upon same issue with typeahead and don't seem to find $setScope in source or anywhere in history for ngModel commits. Right now just using workaround by accessing child controller from parent to read the model value.

navikale avatar Jun 03 '16 15:06 navikale

@NavnathKale This was never included. I've never seen it before, either.

Narretz avatar Jun 03 '16 17:06 Narretz