Last active
March 3, 2016 08:56
-
-
Save Elevista/418a97911536d8ee610f to your computer and use it in GitHub Desktop.
My angular utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function (angular) { | |
var app = angular.module('bj-module', []); | |
/** | |
* @example | |
* <div require-checked tooltip="please select at least one"> | |
* <input type="checkbox" name="chk" value="a"> | |
* <input type="checkbox" name="chk" value="b"> | |
* </div> | |
*/ | |
app.directive('requireChecked', function () { //모든 경우에 쓸 수 있는 디렉티브. 체크박스가 한개 미만 선택시 툴팁 띄움 | |
return { | |
scope: false, | |
link: function (scope, elm, attr) { | |
var chks = elm.find('input[type=checkbox]'); | |
function check() { | |
for (var i = 0; i < chks.length; i++) | |
if (chks[i].checked) | |
return ""; | |
return attr.tooltip; //attribute로 띄울 메세지를 설정 받아야 한다. | |
} | |
function setValidity() { | |
var tooltip = check(); | |
for (var i = 0; i < chks.length; i++) | |
chks[i].setCustomValidity(tooltip); | |
} | |
scope.$watchCollection(function () { | |
var checked = []; | |
for (var i = 0; i < chks.length; i++) | |
checked.push(chks[i].checked); | |
return checked; | |
}, function (newValue, oldValue) { | |
setValidity(); | |
}); | |
setValidity(); | |
} | |
} | |
}); | |
app.directive('validityMsg', function () { | |
return { | |
link: function (scope, elm, attr) { | |
elm[0].setCustomValidity(attr.validityMsg); | |
} | |
}; | |
}); | |
})(angular); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment