2016-06-11 59 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; 
    }); 
}