I have a directive that use keypup to prevent any null value, the min value must be always 1, it also bound to ng-model and ng-change without rely on the directive.
View
<input class="form-control numberOfUsesMustNotNull" type="number" placeholder="Qty" ng-model="qty" ng-change="updatePrice (qty)" />
Directive
.directive('numberOfUsesMustNotNull', function() {
return {
restrict: 'C',
link: function(scope, element, attrs) {
element.bind('keyup', function($event) {
// disallow number of uses to be zero
if(element.val() === '' || element.val() === 0){
element.val(1);
}
});
}
}
});
The problem here is when the condition in the directive been triggered, the updatePrice
function in my controller won't trigger. How do I trigger controller's $scope.updatePrice
from directive? I think I should not just set the DOM value, but have access to $scope.qty
in the directive.
Try this fiddle http://jsfiddle.net/67s2ktfz/, backspace the number, it will changed back to one but the total isn't updated.