2014-03-30 53 views
0

我写了一个服务使用$资源从数据库获取数据:

.factory('Students', ['$resource', function($resource) { 
    return $resource('/students', {}, { 
     query: {method: 'GET', isArray: true} 
    }); 
    }]) 

,但我希望获取不同的数据时,我的网页的URL改变。所以我改成了这样:

factory('Students', ['$resource', function($resource) { 
    var urlBase = '/group-'; 
    return function(urlExt) { 
     var url = urlBase + urlExt; 
     return $resource(url, {}, { 
      query: {method: 'GET', isArray: true} 
     }); 
    } 
}]); 

而且我把它在我的控制器是这样的:

$scope.students = Students($location.path()); 

我没有得到一个错误,但它不返回任何东西。它是否与页面不刷新但加载模板视图有关?

回答

0

我想你可以做这样的:

.factory('Students', ['$resource', '$location', function($resource, $location) { 
    return $resource($location.path(), {}, { 
    query: {method: 'GET', isArray: true} 
    }); 
}]) 

您也可以尝试:

.factory('Students', ['$resource', '$location', function ($resource, $location) { 
    return $resource(':dest', {dest:$location.path()}); 
}]); 

我不知道如果:dest将不带斜线工作,我可以肯定$location.path()有斜线,所以你可能需要将其更改为:

.factory('Students', ['$resource', '$location', function ($resource, $location) { 
    return $resource('/:dest', {dest:$location.path().substr(1)}); 
}]); 

好了,为了防止网址编码错误,请再次尝试第一个,但注入$ sce并将其用于白名单url,这可能与该原始问题有关:

.factory('Students', ['$resource', '$location', '$sce', function($resource, $location, $sce) { 
    return $resource($sce.trustAsResourceUrl($location.path()), {}, { 
    query: {method: 'GET', isArray: true} 
    }); 
}]) 
+0

当我添加$ location作为一个依赖,它给了我一个“糟糕的配置”错误。 – talzag

+0

只是通过添加$ location作为依赖项,或者当您将url设置为$ location.path()时发生错误? – dave

+0

它看起来只是设置URL为$ location.path()可能是那里的问题。只是加载它作为依赖不会给我一个错误。 – talzag

相关问题