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

二次信任 提交于 2019-12-02 17:28:32

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/21717555/why-is-my-angularjs-directive-causing-two-way-binding-to-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!