2015-11-26 46 views
2

我有一個簡單的div(class = blue),只有在定位到'two.html'而不是'one.html'時才顯示,或者ngRoute中的'three.html' - 角度路由。有人可以有解決方案嗎?在ng-view內容的基礎上顯示/隱藏元素

HTML:

<html> 
<head> 
<!-- Start Required --> 
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> 
<script src="http://code.angularjs.org/1.2.1/angular-route.min.js"></script> 
<script src= "angularScript.js"></script> 
<!-- End Required --> 
<script src= "angularScript.js"></script> 
<style> 
.blue {height:300px;width:300px;background:red;} 
</style> 
</head> 

<body ng-app="myApp" ng-controller="myCtrl"> 
<div class="blue"></div> 
Below will be the content: 
<div ng-view=""></div> 
<a href="#/one">One</a> 
<a href="#/two">Two</a> 
<a href="#/three">Three</a> 
</body> 
</html> 

angularScript.js

//App Declaration 
var app = angular.module('myApp', ['ngRoute']); 

//Controllers 
app.controller('myCtrl', function($scope) {}); 
app.controller('oneCtrl', function($scope) {}); 
app.controller('twoCtrl', function($scope) {}); 
app.controller('threeCtrl', function($scope) {}); 

//Routers 
app.config(function($routeProvider){ 

    $routeProvider 
    .when('/one', { 
     title: 'one', 
     controller: 'oneCtrl', 
     templateUrl: 'one.html' 
    }) 
    .when('/two', { 
     title: 'two', 
     controller: 'twoCtrl', 
     templateUrl: 'two.html' 
    }) 
    .when('/three', { 
     title: 'three', 
     controller: 'threeCtrl', 
     templateUrl: 'three.html' 
    }) 
    .otherwise({ 
     title: 'one', 
     redirectTo: '/one' 
    }); 
}); 

one.html

This is one 

two.html

This is two 

three.html

This is three 

有人可以幫我在隱藏的條件選擇/顯示我的路線我的NG-觀點的基礎DIV是繼?

回答

1

你可以在你的MyCntrl,檢查路徑下面給出一個函數"two"它使用$location

$scope.isSecondPage = function(){ 
     if($location.path().search(/^\/two/) == -1) 
     return false; 
     else 
     return true; 
}; 

和/ NG-如果在div你想可以用它在NG-秀有條件地隱藏。

<div class="blue" ng-show="isSecondPage()"></div>