angular directive data binding happens after ng-ch

2019-07-29 13:12发布

Guys I'm experimenting something.

template

<input my-checkbox type="checkbox" ng-model="object.isChecked" ng-change="triggerChange()" ng-click="triggerClick()">

directive my-checkbox (written in coffeescript)

angular.module('myApp')
  .directive('myCheckbox', ()->

    return {
      restrict: 'A'
      replace: true,
      template: """
                <div>
                    <input type="checkbox" ng-model="ngModel" ng-change="ngChange()" ng-click="ngClick()">
                </div>
                """
      scope: {
        ngChange: "&"
        ngClick: "&"
        ngModel: "="
      }

      }
  )

Observation

When you check the checkbox, function triggerChange() fires but, object.isChecked value doesn't change. Then function triggerClick() fires with object.isChecked value changes.

I'm wondering, is it true, the data binding "=" happens after ng-change?

1条回答
霸刀☆藐视天下
2楼-- · 2019-07-29 13:45

See: ngModelOptions

Allows tuning how model updates are done. Using ngModelOptions you can specify a custom list of events that will trigger a model update and/or a debouncing delay so that the actual update only takes place when a timer expires; this timer will be reset after another change takes place.

For example, try this:

<input type="text" ng-model="term" ng-change="fn(term)" ng-model-options="{debounce: 750}" />
查看更多
登录 后发表回答