首先,element.html是一个方法而不是一个属性,所以你覆盖了scope.$watch回调中的函数。其次,每个人(您自己、Angular、浏览器)都更容易更改输入类型:
http://jsfiddle.net/K6Qgm/6/
scope.$watch('viewPasswordCheckbox', function (newValue) {
element.find('input')[1].type = newValue ? 'password' : 'text';
});
编辑:如果您确实需要支持旧版本的 IE(以上适用于 9),您可以将输入渲染两次并像这样显示/隐藏它:http: //jsfiddle.net/K6Qgm/7/
function (newValue) {
var show = newValue ? 2 : 1,
hide = newValue ? 1 : 2,
elements = element.find('input');
elements[hide].style.display = 'none';
elements[show].style.display = '';
});