具有URL参数的AngularJS远程数据

时间:2022-09-05 19:42:14

So for an AngularJS app I'm building, we were previously using local JSON files to import data into the app using routing.

因此,对于我正在构建的AngularJS应用程序,我们之前使用本地JSON文件使用路由将数据导入应用程序。

storiesControllers.controller('StoryDetailCtrl', ['$scope', '$sce', '$http', '$location', '$routeParams', function($scope, $sce, $http, $location, $routeParams) {
    $scope.storyID = $routeParams.storyID;
    $http.get('/story/'+$routeParams.storyID+'.json')
    .success(function(data) {
        $scope.story = data;
    }); 
}]);

However, we're now going to be getting data direct from the server. Only one line changes below:

但是,我们现在将直接从服务器获取数据。下面只有一行更改:

storiesControllers.controller('StoryDetailCtrl', ['$scope', '$sce', '$http', '$location', '$routeParams', function($scope, $sce, $http, $location, $routeParams) {
    $scope.storyID = $routeParams.storyID;
    $http.get('http://website.co.uk/?id='+$routeParams.storyID)
    .success(function(data) {
        $scope.story = data;
    }); 
}]);

This method works at the top level for filtering stories by category:

此方法适用于按类别过滤故事的*方法:

$http.get('http://mywebsite.co.uk/?category=CategoryName')

But on the individual story it's completely blank and no data is loaded.

但就个人故事来说,它是完全空白的,没有加载数据。

The URL and parameter is correct and working fine, the data on the server is fine and matches exactly the local files, so why does this not work?

URL和参数是正确的,工作正常,服务器上的数据很好,并且与本地文件完全匹配,为什么这不起作用?

Am I missing something really obvious?

我错过了一些非常明显的东西吗

1 个解决方案

#1


2  

Ok, the answer was actually found by a colleague of mine. It turns out that instead of:

好吧,答案实际上是由我的一位同事发现的。事实证明,而不是:

$scope.story = data;

I needed to use:

我需要使用:

$scope.story = data[0];

I'm not sure of the relevance of the square brackets 0, but it seems to have fixed it!

我不确定方括号0的相关性,但似乎已经修复了它!

#1


2  

Ok, the answer was actually found by a colleague of mine. It turns out that instead of:

好吧,答案实际上是由我的一位同事发现的。事实证明,而不是:

$scope.story = data;

I needed to use:

我需要使用:

$scope.story = data[0];

I'm not sure of the relevance of the square brackets 0, but it seems to have fixed it!

我不确定方括号0的相关性,但似乎已经修复了它!