2016-06-11 24 views
1

我正在使用角度ui路由器,並且需要根據父級(body)作用域中的變量爲給定狀態顯示不同的模板。但是當我添加這個$scope.data條件它不會觸發。我甚至沒有得到console.log()或任何網址更改。 :(

$stateProvider.state('map1', { 
    url: "/content/sectionI", 
    templateProvider: function($scope, $http, $stateParams) { 
     console.log("Wowie!",$scope.data); 
     if($scope.data.Section_I_Part_1_Complete == "0") 
     { 
     return $http({ 
     method: 'GET', 
     url: 'http://example.com/contentMore', 
     params: { 
      request: "Section_I_Part_1_Complete", 
      part: "1" 
     } 
     }).then(function successCallback(html) { 
     return html.data; 
     }); 
     } 
else 
    { 
     return $http({ 
     method: 'GET', 
     url: 'http://example.com/contentMore', 
     params: { 
      request: "Section_I_Unlocked", 
      part: "map" 
     } 
     }).then(function successCallback(html) { 
     return html.data; 
     }); 
    } 
     }, 
    controller: function($scope) { 
      $scope.activeSection.activeSection = "notNone"; 
     } 
    }) 

我怎麼做,這樣我可以得到模板返回基於變量的父範圍值?

+0

如果我理解正確的話,您要確定動態templateUrl的狀態,您可以通過改變'$ stateChangeStart' templateUrl實現價值即見相關的SO職位:HTTP://計算器。 COM /問題/ 23512238/UI路由器動態模板路徑 –

回答

1

只需使用一個服務來存儲數據,並注入該。服務

templateProvider: function(myDataService) { 

    var part = myDataService.Section_I_Part_1_Complete == "0" ? 1 : 'map'; 
    return $http.get('http://example.com/contentMore', { 
    params: { 
     request: "Section_I_Part_1_Complete", 
     part: part 
    } 
    }).then(function(response) { 
    return response.data; 
    }); 
}