angularjs如何在不更改url的情况下加载另一个控制器
angularjs how to load another controller without change the url
我有一个类似/products/:product_id
的URL
在这个控制器中,我检查产品id是否存在,然后,如果不存在,我想加载一个控制器,它会显示一些文本,比如"找不到产品"。
我正在寻找一些功能,比如:
$location.path('/product_not_found');
但不更改条中的URL
知道吗?
谢谢!
angularjs可以为HTML标记添加属性。在显示产品(如果找到)和消息(如果没有)的情况下,我更喜欢使用ng-show
属性。例如:
使用ng-show不会影响URL,因为内容已经加载。
HTML:
<html ng-app="productDisplay">
<head>
<title>Product info</title>
<script src="js/angular.js"></script>
<script src="js/app.js"></script>
</head>
<body>
<div id="content" ng-controller="ProductController as productCtrl">
<div ng-show="status === 'waiting'">
<p>waiting for product to load..</p>
</div>
<div ng-show="status === 'found'">
<!-- display product info here -->
<p>Name: {{ product.name }}</p>
<p>Price: {{ product.price }}</p>
</div>
<div ng-show="status === 'not-found'">
<p style="color: red;">Not found such product</p>
</div>
</div>
</body>
</html>
JS:
(function() {
// can access varibal app only in this function scope
var app = angular.module('productDisplay', []);
app.controller('ProductController', ['$scope', function($scope) {
$scope.product = {name: '', price: ''};
$scope.status = 'waiting';
this.getProduct = function() {
found = false;
/** some code checking if product found */
if (!found) {
$scope.status = 'not-found';
return {};
} else {
$scope.status = 'found';
return {name: 'BBB', price: '123$'};
}
};
$scope.product = this.getProduct();
}]);
})();
相关文章:
- 在不使用按钮的情况下加载代码
- 如何在没有iFrame的情况下加载Monocle Web电子书/ePub阅读器
- 如何在不重新加载当前页面的情况下加载URL
- 确定是否在不使用任何全局变量的情况下加载了窗口
- 在没有JQuery的情况下加载HTML主体之后调用JavaScript脚本的最佳方式
- 如何在不闪烁的情况下加载Iframe
- 如何在没有查询字符串和参数的情况下加载jQuery
- 确保数据在没有单独请求的情况下加载到视图中
- 如何在不插入 dom 的情况下加载 mbox 内容
- D3 JS - 在没有 HTTP Get 的情况下加载 JSON
- 在没有 css 和 js 的情况下加载页面后.php网址上添加斜杠
- Webpack - 在不创建别名的情况下加载模块
- 如何在不出现异步错误的情况下加载 Gist 代码
- 操作方法确保在不使用 WebFont 加载器的情况下加载@font面
- 在没有 AJAX 的情况下加载 JSON 数据
- 在没有DNS调用的情况下加载Javascript和CSS文件
- 如何在没有本地服务器的情况下加载本地 JSON 以创建 d3 图表
- 在不更改 URL 的情况下加载页面并保持相对路径正常工作
- 如何在没有标记内容的情况下加载谷歌地图
- 如何组织并确保在不将所有脚本和css放在每个页面上的情况下加载它们