i have directive called players use decide how many players @ position on team. have 4 of these, 1 goal keeper, defence, mid-field , forward, might (the + , - increment , decrement buttons):
- forwards: 2 + - mid-field: 4 + - defence: 4 + - goal keeper: 1 +
i validate increment make sure doesn't go on 11, how can access maxplayers validity in class?
require: = '^ngmodel'; restrict: string = 'a'; link: ng.idirectivelinkfn = (scope: any, element: ng.iaugmentedjquery, attributes: any, ngmodel: ng.ingmodelcontroller) => { scope.gettotalplayers = (): number => { return this.playerstateservice.gettotalplayers(); }; function customvalidation(modelvalue: number) { var totalplayers: number = scope.gettotalplayers(); if (totalplayers > 11) { ngmodel.$render = () => { ngmodel.$setvalidity('maxplayers', false); }; } else { ngmodel.$setvalidity('maxplayers', true); } return modelvalue; } ngmodel.$parsers.push(customvalidation);
what want achieve? more specifically, why function number of players checking in outside of directive?
i see 2 ways.
- getting controller function. in controller write
$scope.myform.mymodelcontroller.$parsers[0]
. - create controller or service number of players check function. transfer function directive. is, make logic verification of directive.
examples:
first way.
function examplecontroller($scope){ var validityfunc = $scope.myform.mymodelcontroller.$parsers[0]; }
second way.
in controller:
function examplecontroller($scope){ $scope.validityfunc = function(){ return totalplayers>11;}; }
in directive:
function customvalidation(modelvalue: number) { ngmodel.$setvalidity('maxplayers', !scope.validityfunc());. return modelvalue; } ngmodel.$parsers.push(customvalidation);
Comments
Post a Comment