【问题标题】:Controller is not called when navigate back导航返回时不调用控制器
【发布时间】:2017-04-29 08:19:17
【问题描述】:

我正在使用 angularJS 和 Jade 进行开发。我将 AppController 附加到我的身体标签上。每次我访问一个新页面时,都会调用控制器。目前,我有两种不同的布局。一个有页眉、导航和页脚。另一个唯一的侧边栏。当我访问第一个布局和第二个布局时,AppController 调用了。但是当我从第二个布局导航回第一个布局时。未调用 AppController。

index.jade

extends layout/layout
block content

Layout.jade

html
 head
  // some metas and links
 body(ng-app="myApp", ng-controller="AppController")
  div(ng-show="!profile")
   include header
   include sidemenu
   include mainmenu

  div(ng-show="profile", id="Profile", style="display:none;")
   include profile/sidemenu

  div(ng-cloak, ng-view)
     block content

  div(ng-show="!profile")
   include footer

  div(ng-show="profile", id="Profile", style="display:none")
    include profile/scripts

AppController

// When navigating to /profile it will call the second layout
// Otherwise first layout.

if($location.path().split('/')[1] == "profile") {
    angular.element('#Profile').css('display','block');
    $scope.profile = true;
}else{
    angular.element('#Profile').css('display','none');
    $scope.profile = false;
}

问题

当我从第二个布局导航回第一个布局时。侧边栏(这是第二个布局)与第一个布局重叠。原因是没有调用 AppController,因此没有调用 css 显示。

【问题讨论】:

  • 发布第二个布局

标签: javascript angularjs pug


【解决方案1】:

我认为最好的方法是将控制器代码包装在一个函数周围,并使用 ng-init 指令从两个层调用该函数

$scope.cssFunc = function(){

    if($location.path().split('/')[1] == "profile") {
        angular.element('#Profile').css('display','block');
        $scope.profile = true;
    }else{
        angular.element('#Profile').css('display','none');
        $scope.profile = false;
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多