而不是$watch
,您可以使用element.bind
“输入”事件。喜欢看:
element.bind "input", ->
ctrl.$modelValue
如果你更新 ctrl,你必须使用 $scope.$apply() 来更新视图:
element.bind "input", ->
$scope.$apply ->
ctrl.$modelValue
这是我使用它的示例:
link: ($scope, element, attrs, ngModelController) ->
element.bind "focus", ->
if not ngModelController.pristineBlur and not ngModelController.dirtyBlur
ngModelController.pristineFocus = on
else
ngModelController.dirtyFocus = on
ngModelController.pristineBlur = off
ngModelController.dirtyBlur = off
ngModelController.focus = on
updateController()
element.bind "blur", ->
if ngModelController.pristineFocus
ngModelController.pristineBlur = on
ngModelController.pristineFocus = off
else
ngModelController.dirtyBlur = on
ngModelController.dirtyFocus = off
ngModelController.focus = off
updateController()
element.bind "input", ->
updateController()
updateController = ->
$scope.$apply ->
ngModelController.pristineFocusOrValid = ( ngModelController.pristineFocus and ngModelController.pristineFocus or ngModelController.pristineFocus and ngModelController.$valid ) or ( ngModelController.focus and ngModelController.$valid )
ngModelController.minlengthWarning = ( ( ngModelController.pristineBlur or ngModelController.dirtyFocus or ngModelController.dirtyBlur ) and ngModelController.$viewValue and ngModelController.$invalid ) or ngModelController.dirtyFocus and ngModelController.$invalid
ngModelController.isRequiredError = ( ngModelController.pristineBlur or ngModelController.dirtyBlur ) and not ngModelController.focus and not ngModelController.$viewValue