使用AngularJS删除DOM元素

前端之家收集整理的这篇文章主要介绍了使用AngularJS删除DOM元素前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试根据当前位置路径删除导航栏.

这是我到目前为止:

angular.module('myModule')
  .controller('MainController',function ($location,$document) {
    if ($location.path().indexOf('resetpass') > -1) {
      var navbar = angular.element($document.querySelector(".top-navbar"));
      navbar.remove();
    }
  });

通过这种方法,控制台说:

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,并执行以下操作:

模板:

<element ng-if="hideElemet"></element>

控制器:

if ($location.path().indexOf('resetpass') > -1) {
      $scope.hideElement = false
}

ngIf将从DOM中删除该元素

猜你在找的Angularjs相关文章