.directive('mdValid', function($rootScope, $compile) {
return {
restrict: 'EA',
scope: { mdName: '@'},
link: function (scope, elem, attr) {
console.log("-----");
console.log(scope.mdName);
console.log("-----");
elem.attr("ng-if","false");
}
};
});
为什么设置了无法隐藏
可以去了解一下Angular的总体原理,ng-if是属于compile阶段,Angular拿到模板后解析到ng-if就会去获取表达式的结果,然后决定是否渲染当前dom节点,而elem.attr("ng-if","false")执行的时候dom节点已被渲染结束,此时elem.attr()只能设置dom元素的一些attributes。