Wouter Willems
Wouter Willems

Reputation: 442

angular Accessing ngModel controller in directive with custom template in compile

I want to access the ngModel controller (to later use setValidity to validate the custom input field). However, when I want to use the directive as an attribute (not class), the replaceWith function throws an error that It can not find ngModel controller. I created a fiddle here:

jsfiddle.net/6HcGS/396

Can anybody help me out?

This is related to my first questions:

databinding custom directive angular with replacing html in compile function

Upvotes: 3

Views: 1178

Answers (2)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

Instead

<input zippy ng-model="title"/>

try to write attribute after ng-model like:

 <input  ng-model="title" zippy/>

You interesting to load zippy before ng-model directive.

Like bekite says, set priority: -1

Demo Fiddle

Upvotes: 1

bekite
bekite

Reputation: 3444

<input zippy ng-model="title">

zippy and ngModel are both directives that are render in a specific order. In this case zippy gets rendered before ngModel. You can reorder the directives like Maxim Shoustin suggested or you could specify the rendering order by providing a priority attribute on the directive creation function like so:

  .directive('zippy', function($compile){
    return {
      restrict: 'A',
      priority: -1,
      replace: true,
      ...

The default preority is 0. Directives with higher priorities are rendered first.

Upvotes: 2

Related Questions