/** * Created by NICK on 15/6/19. * email:nick121212@126.com * qq:289412378 * copyright NICK */ import ref= require('ref'); import angualr = require('angular'); export class EqualSomeDirective { public static _name:string = "equalsome"; public static directive:Array = ['$timeout', ($timeout)=> { var directive:ng.IDirective = {}; directive.terminal = true; directive.priority = 0; directive.require = "form"; directive.link = ($scope, $elem, $attrs)=> { var items = $attrs['equalsome'], otherInput; var ctls = []; function parser(value) { var rtn = true, othval; for (var ctl in ctls) { ctl = ctls[ctl]; othval = ctl.$modelValue || ctl.$viewValue; rtn = othval == value; if (!rtn) { break; } } if (rtn) { ctls[ctls.length - 1].$setValidity("10001", true); } else { ctls[ctls.length - 1].$setValidity("10001", false); } } function init() { items = items.split(','); if (items.length > 1) { for (var item in items) { item = items[item]; otherInput = $elem.inheritedData("$formController")[item]; if (otherInput) { ctls.push(otherInput); otherInput.$parsers.push(function (val) { parser(val); return val; }); } } } } $timeout(function () { init(); }, 1000); }; return directive; }]; };