使用 AngularJS 删除 DOM 元素 [英] Remove DOM-Element with AngularJS
本文介绍了使用 AngularJS 删除 DOM 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试根据当前位置路径删除导航栏.
I'm trying to remove a navbar based on the current location path.
这是我目前所拥有的:
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屋!
查看全文