2016-10-18 118 views
0

我已經用UI-Router multiple named views not working的頂級解決方案與我的角度應用程序差不多,但我的UI視圖似乎沒有更新,而只是完全消失。必須有一些非常小的細節,我錯過了很長一段時間...Angularjs ui-view沒有更新

我有一個index.html頁面,[div ui-view =「」]在主場換下。這個ui-view(frontpage.html)有另一個[div ui-view =「search-result」],我希望從狀態變化中更新(將文本更改爲「成功的模板替換」),但是當狀態改變時,當點擊搜索按鈕時,index.html中的整個[div ui-view =「」]消失。

所有相關的腳本標籤都包含在我的index.html中。

的index.html:

<body ng-app="..."> 
<div class="container"> 
    <div ui-view=""></div> 
</div> 

app.js:

angular.module('...', [ 'ui.router','ui.bootstrap']). 
config(function($stateProvider, $urlRouterProvider) { 
$urlRouterProvider.otherwise('/'); 
$stateProvider 
    .state('main', { 
     url: "/", 
     templateUrl: "/app/states/frontpage/frontpage.html" 
    }) 
    .state('search', { 
     url : "/search/:query", 
     views: { 
      '[email protected]': { 
       template: 'successful template replacement' 
      } 
     } 
    }).run(function(){}); 

frontpage.html:

<div class="row" ng-controller="MainController"> 
    some stuff here 

    <div class="col-xs-9"> 
     <input type="text" class="inline" ng-model="searchText" placeholder="Search..."></input> 

     <button class="btn btn-default inline" type="button" ng-click="search()">Search</button> 

     <div ui-view="search-result"></div> 
    </div> 
</div> 

mainCtrl.js

angular.module('...') 
.controller('MainController', ['$scope','$state', function($scope,$state) { 
    $scope.searchText; 

$scope.search = function(){ 

    console.log('going to state search with searchText: ' + $scope.searchText); 
    $state.go('search',{query:$scope.searchText}); 
} 

}]); 

回答

0

在情況下'search'狀態應該去模板'main' ...它必須是其子(檢查parent: 'main'

.state('main', { 
    url: "/", 
    templateUrl: "/app/states/frontpage/frontpage.html" 
}) 
.state('search', { 
    parent: 'main', // parent is main, named view will find its target 
    url : "/search/:query", 
    views: { 
     '[email protected]': { 
      template: 'successful template replacement' 
     } 
    } 
} 
+0

我加父線到「搜索「狀態。現在沒有任何反應(至少從頁面上消失)。還嘗試通過刪除初始斜槓來更改搜索網址爲「search /:query」,因此URL頂部欄中沒有2個斜槓 – user3125693