2013-09-25 26 views
0

我已經爲許多靜態頁面設置了一條特殊的路線,這是一條單一路線(想想隱私政策,客戶端html,我希望我的客戶能夠在文件夾的HTML頁面,並將其與路由動態加載。動態地更改路線並附加內容

當您在我告訴角度去在目錄中查找並找到的.html文件$ routeParams.name地址欄的東西並加載它

這很好,但我將數據替換到一個空白頁面blank.ht毫升

App.js

.config(function($routeProvider, $locationProvider) { 
$locationProvider.html5Mode(true); 

$routeProvider.when('/test', { 
    controller : 'AppRepeatCtrl', 
    templateUrl : './templates/test_tpl.html' 
}).when('/:name', { 
    controller: 'DynamicRoutes', 
    templateUrl: 'partials/blank.html' 
}).otherwise({ 
    redirectTo: '/' 
}); 

})

Controllers.js

function DynamicRoutes ($scope, $http, $location, $route, $routeParams, $compile, appLoading) { 
    if ($routeParams.name == '') { 
    $route.current.templateUrl = 'partials/' + "home.html"; 
    } else { 
    $route.current.templateUrl = 'partials/' + $routeParams.name + ".html"; 
    } 

    $http.get($route.current.templateUrl).then(function (msg) { 
    if (msg.data.contains('html')) { 
     $http.get('partials/error.html').then(function (msg) { 
      $('#ng-view').html($compile(msg.data)($scope)); 
     }); 
    } else { 
     appLoading.ready(); 
     $('#ng-view').html($compile(msg.data)($scope)); 

    } 
}); 
}; 

我的問題是, 有什麼辦法來加載頁面了從部分,而不必加載空白模板然後用靜態頁面替換html。因爲我正在做動畫,所以動畫因爲替換而變得臃腫。我真的需要在頁面加載,而無需更換blank.html模板頁面,或更換包含blank.html意見之前開始NG-動畫

$(「#NG-視圖」)。HTML($編譯(msg.data)($範圍));是我認爲是導致頁面取代html沒有ng-animate輸入視圖完成。

因爲如果您將它更改爲$('#ng-view')。append($ compile(msg.data)($ scope));

你可以看到2 x和1有動畫,但只要追加發生動畫停止。

回答

5

首先,不要在控制器中加載模板,它不是以角度的方式來完成它。

您不需要加載blank.html,因爲您必須提供一個模板,templateUrl可以是一個函數,可以確定要加載的模板。

$routeProvider.when('/:name', { 
    controller : 'DynamicRoutes', 
    templateUrl : function (params) { 
     console.log(params); // check the console to see what are passed in here 

     //return a valid Url to the template, and angular will load it for you 
    } 
}); 
+0

熱潮!謝謝!我知道這是一條正確的道路。 – Zuriel

+0

我知道評論有點晚,但是,你能解釋什麼時候該函數被調用(我想了解何時可以傳遞參數)。 – JimmyBoy