angular.js - angularjs 中ui-sref里的地址會和瀏覽器里的不一致
問題描述
我在angular里面使用了ui-router, 在a標簽中使用了ui-sref,但是現在發現一個情況就是,有時候莫名其妙瀏覽器地址欄里的參數會和標簽里寫的不一致,而且時好時壞,不知道問題出在哪里了。
點擊a標簽,會跳轉到另一個視圖上去的。不知道這樣寫對不對,主要是這個現象會突然出現,毫無規律可循,地址欄的變化都是靠ui-sref去改變的,也沒有使用別的東西去修改url。。
路由代碼
.state(’chschool’,{ url: ’/chschool’, views: {’’: { templateUrl: ’templates/chschool.html’ //總視圖 內嵌了頂部和右側學校部分},’indexheader@chschool’:{ templateUrl: ’templates/indexheader.html’ //頂部},’itemcontent’:{ templateUrl: ’templates/schoolpart.html’ //學校部分} }}).state(’chschool.school’,{ //修改學校 用來局部刷新 學校部分 這里根據路由參數 url: ’/school/{id}’, views: {’itemcontent’:{ templateUrl: ’templates/schoolpart.html’} }}).state(’chdorm’,{ //進入樓棟的頁面 需要根據傳入的學校參數來獲取對應的樓棟,這里就碰到了傳入的學校id會和schoolpart.html里面用ng-repeat循環出來的ui-sref中的參數不同 url: ’/chdorm/{id}’, views: {’’: { templateUrl: ’templates/chdorm.html’ } }})
html 部分 總視圖
<section ui-view='indexheader'></section><p ng-show='hidep' class='yo-container'> <p class='yo-xs-left'><p > <p ng-include='’templates/citypart.html’'></p> //城市</p> </p> <p class='yo-xs-right'><p class='yo-school'> <!--<p ng-include='’templates/schoolpart.html’'></p>--> <p ui-view='itemcontent'></p> //學校部分</p> </p></p>
citypart.html
<a ng-repeat='item in items' ui-sref='.school({id:item.ID})' ui-sref-active='yo-active'> {{item.cityname}}</a>
schoolpart.html
<p ng-controller='chschoolController'> <p ng-if='items.length==0'>團隊招募,期待你的加入</p> <p ng-repeat='item in items track by $index' ng-cloak><a ui-sref='chdorm({id:item.ID})' ng-click='setItem($index)'>{{item.schName}}</a> </p></p>
問題解答
回答1:最好把壞的情況是什么樣式貼出來,題外話,你js可以稍微優化
.state(’app.table.datatable’, { url: ’/datatable/:id’, templateUrl: function (stateParams) {console.log(stateParams);return ’resources/tpl/test/table_static.html’; }, resolve: {loadMyCtrl: [’$ocLazyLoad’, function ($ocLazyLoad) { return $ocLazyLoad.load(’resources/js/controllers/dataTableDemo.js’);}] } })回答2:
能放上出現錯誤的截圖嗎?如果需要定位的話,可以在config里面監聽路由切換事件$stateChangeStart,并查看路由變換記錄:
$rootScope.$on(’$stateChangeStart’, function(event, toState, toParams, fromState, fromParams){ console.info(fromState + '->' + toState, toParams, fromParams); })
相關文章:
1. golang - 用IDE看docker源碼時的小問題2. nignx - docker內nginx 80端口被占用3. javascript - vue使用videojs+videojs-contrib-hls報錯4. io - java 文件操作,如何向指定的位置插入內容 (不是替換內容) ?5. javascript - JS 里面的 delete object.key 到底刪除了什么?6. javascript - webpack --hot 熱重載無效的問題7. dockerfile - [docker build image失敗- npm install]8. Python 爬蟲 遇到的問題(手淘問大家)9. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””10. mysql如何配置遠程php外網鏈接數據庫
