angularJS:如何条件显示ng-view之外的元素?

时间:2017-04-20 12:19:35

标签: angularjs

考虑以下有角度的1.X index.html文件:

<div ng-app="app" class="hero-unit">
    <a ng-show="!isHomePage" href="#!/home">Home</a>
    <div class="container-fluid">
        <div ng-view></div>
    </div>
</div>

我想仅在位置不是主页时才显示元素<a ng-show="!isHomePage" href="#!/home">Home</a>? 我该怎么办?我应该将此元素作为mainController映射到专用控制器上吗?这是一个好习惯吗?

3 个答案:

答案 0 :(得分:1)

实现这一目标的方法:

  • 您可以在应用程序中创建 import sys import time from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5 import uic import Con_pos import error_log as error class Registro(QMainWindow): con = Con_pos.con_pos() def __init__(self): QMainWindow.__init__(self) uic.loadUi("Registro.ui", self) self.btn_reg.clicked.connect(self.validar_nombre) def validar_nombre(self): fecha = time.strftime("%X") + " " + time.strftime("%x") usuario = self.user_tx_input.text() password = self.pass_tx_input.text() if usuario != "": pid = self.getPID() consul = '''SELECT "id_user" FROM "User" WHERE "id_user" = '%s'; ''' % (usuario) res_con = Registro.con.consulta(consul) if not res_con: ins_user = '''INSERT INTO "User" VALUES ('%s', '%s') ''' % (usuario, password) ins_sesion = ''' INSERT INTO "sesion" VALUES ('%s', %s,'%s') ''' % (fecha, pid, usuario) Registro.con.insertar(ins_user) Registro.con.insertar(ins_sesion) else: err = error.error_dia() err.etiqueta.setText("El usuario %s ya esta en uso" % (usuario)) err.exec() def getPID(self): func = "pg_backend_pid" pid = Registro.con.call_proc(func) pid = pid[0][0] return pid if __name__ == "__main__": app = QApplication(sys.argv) ventana = Registro() ventana.show() sys.exit(app.exec()) ,然后检查当前路由并根据该实现条件。

        家                   

要获取当前路径,您可以使用:

parent level controller(outside ng-view)

<强> app.run(function ($rootScope) { $rootScope.$on('$routeChangeSuccess', function (e, current, pre) { console.log(current.originalPath); // Do not use $$route here it is private }); });

OR

  • 如果您想根据视图中的任何事件进行操作,可以使用 $location.path() $scope.$emit将有助于孩子与父母之间的沟通。
  • 您可以创建一个$emit,您可以传递应用程序的当前状态,并根据该实现条件。

答案 1 :(得分:0)

尝试将ng-app移动到正文级别,然后使用$ rootScope中的服务来检测它是否是主页 - 我在某些应用中执行此操作。

e.g。

<body ng-app="app">
<div class="hero-unit">
<a ng-show="!NavigationService.isHomePage()" href="#!/home">Home</a>
<input id="token" type="hidden" value="{{token}}">
<div class="container-fluid">
    <div ng-view></div>
</div>

    

然后在你的app.js run方法中,将NavigationService设置为rootScope,

e.g。

$rootScope.NavigationService = NavigationService;

答案 2 :(得分:0)

您可以查看当前网址: 请参阅以下示例: https://docs.angularjs.org/api/ngRoute/directive/ngView

 <div ng-controller="MainCtrl as main">
    <div class="view-animate-container">
      <div ng-view class="view-animate"></div>
   </div>
    <hr />

  $location.path() = {{main.$location.path()}}
  $route.current.templateUrl = {{main.$route.current.templateUrl}}
  $route.current.params = {{main.$route.current.params}}
  $routeParams = {{main.$routeParams}}
 </div>