Angularjs用不同的url调用实例化的资源方法
Angularjs calling instantiated resource methods with different url
我试图用相同的实例化对象对多个端点进行资源调用。使用原型资源方法的方法确实会更改端点,但是@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;
};
将这段代码放在注释部分可能真的很难,所以作为答案添加。
相关文章:
- 如何在GoogleWeb工具包(GWT)中从JSNI调用接口(实例化)
- 如何在用户输入时实例化数组
- 在requirejs中共享实例化对象
- Mongoose TypeError:实例化模式类型的对象时,对象不是函数
- Angular重命名模块后未能实例化模块
- Backbone relational无法实例化两个RelationalModel对象
- 我应该如何用javascript实例化这个日期
- AngularJS出错-无法实例化模块
- 谷歌地图重叠MarkerSpiderfier实例化问题
- 未捕获错误:由于,[$injector:moduler]未能实例化模块polmgr
- ngRoute不是因果报应实例化的
- JavaScript 中的动态实例化
- 在文本框上绑定谷歌地点自动完成,而无需实例化谷歌地图
- UI5 路由不实例化视图
- 在 JavaScript 中,当我们实例化派生对象时,原型的函数隐藏在哪里
- 仅在需要时实例化聚合物元素
- 角度 JS 控制器未实例化
- 如何在资源实例化后拦截Angularjs resource'的动作
- Angularjs用不同的url调用实例化的资源方法
- Angular 的错误消息“无法实例化模块”仅在资源管理器中