javascript - AngularJS 嵌套 $state 问题

标签 javascript html angularjs angular-ui-router

  $stateProvider.state('brands', {
    url: '/brands',
    controller: 'brandsController',
    templateUrl: 'views/brands/brands.html'
  });

  $stateProvider.state('brands.details', {
    url: '/details/:uid',
    controller: 'brandController',
    templateUrl: 'views/brand/brand.html',
    params: { brand: null, product: null }
  });

使用

  <a ui-sref="brands.details({ uid: brand.uid, brand: brand, product: product })">{{ product.name }}</a>

总是重定向到父级。为什么?

最佳答案

它确实工作正常。只需配置您的 statesconfig一劳永逸地成为您申请的一部分。请检查这个running plnkr demo并将其与您的解决方案进行比较。确保你有 <div ui-view=""></div>在你的父 View 中。

索引 View

<!DOCTYPE html>
<head>
  <!-- Angular -->
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js"></script>
  <!-- UI-Router -->
  <script src="//angular-ui.github.io/ui-router/release/angular-ui-router.js"></script>
  <script src="script.js"></script>
</head>

<body data-ng-app="myApp">
  <h2>AngularJS Ui router - Demonstration</h2>
  <div data-ui-view=""></div>
</body>

</html>

品牌 View

<h1>Brands</h1>
<div>
     <div>
         <span ui-sref="brands.details({uid: brand.uid, brand: brand, product: product})">
             <a href="">Brand</a>
         </span>
     </div>
     <div>
         <div ui-view=""></div>
     </div>
</div> 

AngularJS 应用

var myApp = angular.module("myApp", ['ui.router']);

myApp.config(function ($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.when("", "/brands");

    $stateProvider
        .state("brands", {
            url: "/brands",
            controller: 'brandsController',
            templateUrl: "brands.html"
        }).state('brands.details', {
            url: '/details/:uid',
            controller: 'brandController',
            templateUrl: 'brand.html',
            params: { brand: null, product: null }
        });
}); 

myApp.controller('brandsController', function () {

});

myApp.controller('brandController', function () {

});

关于javascript - AngularJS 嵌套 $state 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43448411/

相关文章:

javascript - 这是根据 angularjs 中的根组织显示和隐藏 html 的代码的好方法吗

javascript - 将字符串 "true"/"false"转换为 boolean 值

javascript - Jquery 幻灯片 ALT 文本

php - 如何在 PHP、HTML 标签中回显

javascript - 尝试检查 div 标签中的文本并附加结果

javascript - IE 中的模板元素解决方法

javascript - HTML5 放置目标支持哪些剪贴板格式?

javascript - 同一HTML页面的横向和纵向设计

javascript - AngularJS - 注入(inject)提供者

AngularJS ng-include 不包含 View ,除非传入 $scope