使用AngularJS删除DOM元素 [英] Remove DOM-Element with AngularJS
本文介绍了使用AngularJS删除DOM元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试根据当前位置路径删除导航栏.
I'm trying to remove a navbar based on the current location path.
这是我到目前为止所拥有的:
This is what I have so far:
angular.module('myModule')
.controller('MainController', function ($location, $document) {
if ($location.path().indexOf('resetpass') > -1) {
var navbar = angular.element($document.querySelector(".top-navbar"));
navbar.remove();
}
});
通过这种方法,控制台会显示:
With this approach the console says:
angular.js:14110 TypeError: $document.querySelector is not a function
at new <anonymous> (main.controller.js:6)
at Object.invoke (angular.js:4762)
at $controllerInit (angular.js:10518)
at nodeLinkFn (angular.js:9416)
at compositeLinkFn (angular.js:8757)
at compositeLinkFn (angular.js:8760)
at publicLinkFn (angular.js:8637)
at angular.js:1808
at Scope.$eval (angular.js:17913)
at Scope.$apply (angular.js:18013)
我在做什么错了?
推荐答案
在DOM
元素中使用ngIf
并执行以下操作:
use ngIf
in your DOM
element and do something like this:
模板:
<element ng-if="hideElemet"></element>
控制器:
if ($location.path().indexOf('resetpass') > -1) {
$scope.hideElement = false
}
ngIf将从DOM中删除该元素
ngIf will remove the element from the DOM
这篇关于使用AngularJS删除DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文