Javascript Angularjs使用不同的url调用实例化的资源方法

Javascript Angularjs使用不同的url调用实例化的资源方法,javascript,angularjs,angularjs-directive,angularjs-service,angular-resource,Javascript,Angularjs,Angularjs Directive,Angularjs Service,Angular Resource,我正在尝试使用同一实例化对象对多个端点进行资源调用。使用原型资源方法的方法确实会更改端点,但@id无法提取。id确实是对象上的一个属性,如果我使用常规模型。$get,它可以工作。我感谢任何帮助,我希望代码中的注释能更好地帮助解释 admin.controller('ModelController', ['$scope', 'Model', function($scope, Model) { // first call to instantiate the models with the

我正在尝试使用同一实例化对象对多个端点进行资源调用。使用原型资源方法的方法确实会更改端点,但@id无法提取。id确实是对象上的一个属性,如果我使用常规模型。$get,它可以工作。我感谢任何帮助,我希望代码中的注释能更好地帮助解释

admin.controller('ModelController', ['$scope', 'Model', function($scope, Model) {

    // first call to instantiate the models with the default resource url
    Model.query(
        {},
        function(resp) {
            $scope.models = resp.results;
        }
    );

    // this event is emited from a directive
    // data is a object from the models collection
    $scope.$on('modelClick', function(event, data) {

        // without this, event fires twice, i am not sure why.
        event.stopPropagation();


        // creates model with resource prototype
        // the hope is then to be able to do $get or $save on $scope.model
        $scope.model = new Model(data);


        // call below will work normal, pulls the id out of the object
        // and sends it with the request to the default url
        $scope.model.$get();


        // this method changes the resource url to the correct one
        // but id=undefined is put on the query string
        // i have debugged the resource api and when this gets called
        // the object is being sent to the $parse method and the id is available
        $scope.model.getInfo();


        // updates the view
        $scope.$apply('model');
    });


}]);


admin.factory( 'Model', ['$resource', function($resource) {

    // default resource url
    var Model = $resource('/:url', {id: '@id', url: 'initial/path'}, {});


    // method to call a different endpoint, but with same id from instantiated object
    Model.prototype.getInfo = function(params, success, error) {
        var response = this.$get(
            {url: 'info/path'}
        );
        return response;
    };

    return Model;

}]);

对于$scope.model.getInfo()调用/info/path?id=undefined的原因,如有任何帮助,将不胜感激。谢谢。

我不确定这是否有效,但你能试试这个吗

Model.prototype.getInfo = function(params, success, error) {
       var id = this.id; // if id is not a direct member of 'this'. Please inspect the 'this' for any getter method to get the member variables.
        var response = this.$get(
            {url: 'info/path', 'id':id}
        );
        return response;
    };

这段代码很难放在评论部分,因此作为答案添加。

很好的呼叫,这实际上是我正在做的工作,所以我没有被这段代码阻止。既然工作环境很好,我就接受这个答案。但是如果有人知道为什么“@id”没有被提取,请告诉我。为了避免这个id=undefined,你需要指定id在URL中的位置。试试这个:
var Model=$resource('/:url',{id:'@id',url:'initial/path/:id'},{})我使用的是lame服务,它要求id位于查询字符串上,而不是像您在路径中所说的那样。