我希望创建一个包含多个标签的 html 文件。这些应该作为单独的单独视图,通常保存在部分文件夹中。
然后我希望在路由控制器中指定它们。
现在我正在做如下:
应用程序.js
angular.module('productapp', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/productapp', {templateUrl: 'partials/productList.html', controller: productsCtrl}).
when('/productapp/:productId', {templateUrl: 'partials/edit.html', controller: editCtrl}).
otherwise({redirectTo: '/productapp'});
}],
['$locationProvider', function($locationProvider) {
$locationProvider.html5Mode = true;
}]);
索引.html
<!DOCTYPE html>
<html ng-app = "productapp">
<head>
<title>Search form with AngualrJS</title>
<script src="../angular-1.0.1.min.js"></script>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="js/products.js"></script>
<script src="js/app.js"></script>
</head>
<body>
<div ng-view></div>
</body>
</html>
在部分文件夹中:
我有 2 个名为 edit.html 和 Productlist.html 的 html 视图
我不想创建这两个文件,而是希望将它们单独合并为一个文件,并通过路由调用它们(div)。
我该怎么做呢?
您可以使用 ng-switch 有条件地渲染带有 include 的 ProductList,具体取决于路由参数。
在您的配置中尝试一下:
angular.module('productapp', [])
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/productapp', {templateUrl: 'partials/productList.html', controller: productsCtrl})
.when('/productapp/:productId', {templateUrl: 'partials/productList.html', controller: productsCtrl})
.otherwise({redirectTo: '/productapp'});
在你的控制器中:
function productsCtrl($scope, $routeParams) {
$scope.productId = $routeParams.productId;
}
在你的html中:
<...productListHtml...>
<div ng-switch="productId != null">
<div ng-switch-when="true" ng-include="'partials/product.html'">
</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)