Can I use ng-model with isolated scope?

Replacing scope: true with scope: { datetime1: '=ngModel'} in your first fiddle seems to work fine — fiddle. Unfortunately, the link to your “example” fiddle is broken, so I’m not sure what you tried there.

So, it would seem that ngModelController can be used with an isolate scope.

Here’s a smaller fiddle that uses ng-model in the HTML/view, an isolate scope, and $setViewValue in the link function: fiddle.

Update: I just discovered something rather interesting: if the isolate scope property is given a different name — e.g., say dt1 instead of datetime1 — scope: { dt1: '=ngModel'} — it no longer works! I’m guessing that when we require: 'ngModel', the ngModelController uses the name in the HTML/view (i.e., the ng-model attribute value) to create a property on the isolate scope. So if we specify the same name in the object hash, all is well. But if we specify a different name, that new scope property (e.g., dt1) is not associated with the ngModelController we required.

Here’s an updated fiddle.

Leave a Comment