简体   繁体   English

Angular JS,如何将URL参数传递给angularJS中的$ resouce?

[英]Angular JS, how to pass URL parameters to $resouce in angularJS?

Hi I have a simple controller where it passes unique integers into my url, but Im running to many issues. 嗨,我有一个简单的控制器,它将唯一的整数传递到我的网址,但我遇到了很多问题。 I need to change this "4401" dynamically from my controller. 我需要从我的控制器动态更改“4401”。

the url Im trying to reach: https://itunes.apple.com/us/rss/topmovies/limit=50/genre=4401/json 我试图访问的网址: https//itunes.apple.com/us/rss/topmovies/limit=50/genre=4401/json

app.factory('classic',function ($resource) {

    return $resource('https://itunes.apple.com/us/rss/topmovies/limit=50/genre=:id/json', {
        get: {
            method: 'JSONP',
            id: '@id'
        }
    });

});

and here is my controller 这是我的控制器

app.controller('TestCrtl', function ($scope, classic) {
    init();
    function init(id) {
        $scope.movies = classic.get(id);
    }
    $scope.classicMovies = function(){
        var id = "4403";
        init(id);
    }


    $scope.anctionMovies = function(){
        var id = "4404";
        init(id);
    }
});

The error Im getting Uncaught SyntaxError: Unexpected token : 错误我得到Uncaught SyntaxError:意外的令牌:

any help would be highly appreciated. 任何帮助将受到高度赞赏。

 <div class="col-sm-4">
            <button type="button" data-ng-click="actionMovies()" class="btn btn-default">Action</button>
        </div>
        <div class="col-sm-4">
            <button type="button" class="btn btn-default">Scary</button>
        </div>

Try this: 尝试这个:

app.factory('classic',function ($resource) {

    return $resource('https://itunes.apple.com/us/rss/topmovies/limit=50/genre=:id/json', {
        get: {
            method: 'JSONP',
            id: '@id'
        }
    });

});

And in controller change to : 并在控制器中更改为:

$scope.movies = classic.get(id);

I believe this is the correct way to implement parameters when using a resource factory: 我相信这是在使用资源工厂时实现参数的正确方法:

app.factory('movieService',function ($resource) {
    return $resource('https://itunes.apple.com/us/rss/topmovies/limit=50/genre=:id/json', {id: '@id'}, {
        query: { method: 'GET', isArray:true, params: {id: '@id'} }
    });
});

This can be simplified to: 这可以简化为:

app.factory('movieService',function ($resource) {
    return $resource('https://itunes.apple.com/us/rss/topmovies/limit=50/genre=:id/json', {id: '@id'});
});

To call this get method you would need to do the following. 要调用此get方法,您需要执行以下操作。 Note the parameters that are used in the get method. 请注意get方法中使用的参数。

app.controller('TestCrtl', function ($scope, movieService) {

    $scope.classicMovies = function(){
        movieService.query({id: 4403}, function(result){
            $scope.movies = result;
        });
    }
    $scope.anctionMovies = function(){
        movieService.query({id: 4404}, function(result){
            $scope.movies = result;
        });
    }
});

Additionally, it should be noted that the resource method call is going to return a promise. 另外,应该注意资源方法调用将返回一个promise。 You can either set it from the return value of the get method, like you did above (The status of the promise isn't guaranteed), or you can set it in the callback, which guarantees that the promise is resolved. 你可以从get方法的返回值设置它,就像你上面所做的那样(无法保证promise的状态),或者你可以在回调中设置它,这可以保证promise得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM