mikew
mikew

Reputation: 920

Why is my angularjs directive causing two-way binding to fail?

I can't figure out why when my directive is enabled on an element, the two-way binding fails.

Consider this plunkr

On the first removing the tooltip={{input1Error}} makes the input1 variable update as soon as you type in a valid email.

When tooltip={{input1Error}} is added, when typing in a valid email, the input1 model is never updated.

What am I missing?

Upvotes: 0

Views: 298

Answers (1)

Adam Zuckerman
Adam Zuckerman

Reputation: 1641

There is a documented issue with the scope of the controller. You can get around this by implementing the changes below.

Change your controller to this:

app.controller('ctrl1', ['$scope','$log', function($scope, $log) {
    $scope.model = {};
}]);

And the form to:

<form name="myForm" novalidate>

  <div class="form-group">
    <label>Input 1 *</label>
    <input 
      class="span2" 
      name="input1id" 
      type="email" 
      ng-model="model.input1" 
      tooltip="{{model.input1error}}" 
      tooltip-placement="bottom"
      tooltip-trigger="openPopup"
      tooltip-trigger-on='openPopup'
      tooltip-trigger-off='closePopup'
      tooltip-show="myForm.input1id.$invalid"
      required
    />
    <pre>Input 1 is invalid: {{myForm.input1id.$invalid}}</pre>
    <pre>Input 1 valid email: {{!myForm.input1id.$error.email}}</pre>
    <pre>Input 1 error msg: {{model.input1error}}</pre>
  </div>
  <span class='error hidden' error-on="!myForm.input1id.$error.email" error-for='input1error'>Please enter a valid email</span>
  <span class='error hidden' error-on="!myForm.input1id.$error.required"" error-for='input1error'>This field is required</span>
</form>

Upvotes: 1

Related Questions