Angular ui-router ui-sref重定向回调用页面,即使目标页面正确呈现

时间:2014-07-31 16:33:19

标签: angularjs angular-ui-router

我是一个新手,必须在这里做一些愚蠢的事情,而且我无法弄清楚我的生活。在此先感谢您的帮助!

简而言之,我在浏览器中通过地址栏直接访问时,有3个页面都正确加载:

#/communities
#/companies
#/companies/:id

我使用ui-router来定义公司状态如下:

//companies.js file:
.config(function config( $stateProvider ) {
$stateProvider
    .state( 'companies', {
        url: '/companies',
        views: {
            "main": {
                templateUrl: 'companies/companies.tpl.html'
        }
    },
data:{ pageTitle: '' }
  });
})

//companies.detail.js file:
.config(function config( $stateProvider ) {
$stateProvider
    .state( 'companies.detail', {
        url: '/:id',
        views: {
            //use @ to force use of parent ui-view tag
            "main@": {
                templateUrl: 'companies/companies.detail/companies.detail.tpl.html'
        }
    },
data:{ pageTitle: '' }
  });
})

.controller('CompanyDetailCtrl', ['CompaniesService', '$stateParams',
    function(CompaniesService, $stateParams) {

    alert($stateParams.id); //this fires when I navigate to page briefly

    var _this = this; //need to get better approach for this (ideally remove)
    _this.hoas = [];

    CompaniesService.getCompanyHoas($stateParams.id).then(function(response) {
        _this.hoas = response;
    });

}])
;

//communities.tpl.html calling page snippet:
<a ui-sref="companies.detail({id:hoa.company.id})">{{hoa.company.name}}</a>

当我将鼠标悬停在浏览器中时,会正确创建网址(例如:&#34;公司/ 223&#34;)并且公司/ 223页面只能在一瞬间正确导航到BUT,然后重定向回到调用页面。该页面在浏览器历史记录中被捕获(因此我可以回到它并继续使用它并且它完美地运行),但是当我单击此链接时它总是重定向回来。

我缺少什么?它杀了我。谢谢你的帮助。 :)

1 个答案:

答案 0 :(得分:3)

状态始终重定向回调用页面的原因是因为我将uh-sref包装在指向调用页面的父ui-sref元素中:

<a ui-sref="companies" class="list-group-item" 
ng-click="list.selectCommunity(community)" 
ng-switch on="list.isSelected(community)"
ng-class="{'active': list.isSelected(community)}" >
    <div ng-switch-when="true">
        <div class="table-responsive">
            <table class="table">
                ...
                    <td class="hidden-xs"><a ui-sref="companies.detail({id:hoa.company.id})">{{hoa.company.name}}</a></td>
                ...
            </table>
        </div>
    </div>
</a>

我通过将第一行改为:

轻松纠正了这一点
<a href="#" class="list-group-item" ...

希望这可以帮助将来的某个人。我只是希望我没有假设问题出在我的JS中!