我希望在我的应用程序 View 的标题中有一个返回登录页面的链接,但显然不在登录页面本身上。我如何在 Angular.js 中最佳地实现它?
我应该使用$location.url()来确定 View 还是应该使用bind-html或其他东西?
感谢您的一些提示和帮助!
编辑
我当前的代码,我认为我已经让它变得更容易了,因为每个 View 都有自己的 Controller ,但是链接始终显示:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<style>
</style>
</head>
<body ng-app="myApp">
<div data-role="page" id="pageone">
<div data-role="header">
<h1>WELCOME!</h1>
<a href="home" ng-show="!isLandingPage">BACK TO HOME</a>
</div>
<div ng-view></div>
<div data-role="footer">
<h1>footer</h1>
</div>
</div>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"></script>
<script>
'use strict';
var app = angular.module('myApp', ['ngRoute']);
app.config(function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$locationProvider.hashPrefix('!');
$routeProvider
.when('/', {
templateUrl : 'views/home.html',
controller : 'HomeController'
})
.when('/other', {
templateUrl : 'views/other.html',
controller : 'OtherController'
});
});
app.controller('HomeController', function($scope, $http, $location, $route) {
$scope.isLandingPage = true;
});
app.controller('OtherController', function($scope, $route) {
$scope.info = 'Other';
});
</script>
</body>
</html>
最佳答案
该链接始终显示,因为链接所在的 div 没有附加任何 Controller 。
你可以这样做:
app.controller('landingPageCtrl', ['$scope', '$location', function($scope, $location){
$scope.isLandingPage = function(){
return ($location.url() == '/home') ? true : false;
}
}]);
然后使用ng-show根据位置隐藏或显示链接
<div ng-controller="landingPageCtrl" data-role="header">
<h1>WELCOME!</h1>
<a href="home" ng-show="!isLandingPage()">BACK TO HOME</a>
</div>
关于javascript - Angular.js - 根据 View 显示链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33196572/