admin管理员组文章数量:1335380
Hi I'm still new to AngularJs and was wondering if this was possible.
On my controller, I'm trying to create a function that takes a string parameter that will indicate which $http.get to call. I would then like to assign that parameter in my scope. For example
$scope.getpartial = function(partialtype) {
var promise = "";
switch(partialtype) {
case "account":
promise = $http.get("account url here");
break;
case "contact":
promise = $http.get("contact url here");
break;
}
promise.then(function(payload) {
$scope.XXXXXXX = payload.data;
});
}
Where XXXXXXX = partialtype == "account" or "contact"
so the result would be placed and stored under $scope.account and/or $scope.contact.
Is this possible or is there a better way to do this?
Hi I'm still new to AngularJs and was wondering if this was possible.
On my controller, I'm trying to create a function that takes a string parameter that will indicate which $http.get to call. I would then like to assign that parameter in my scope. For example
$scope.getpartial = function(partialtype) {
var promise = "";
switch(partialtype) {
case "account":
promise = $http.get("account url here");
break;
case "contact":
promise = $http.get("contact url here");
break;
}
promise.then(function(payload) {
$scope.XXXXXXX = payload.data;
});
}
Where XXXXXXX = partialtype == "account" or "contact"
so the result would be placed and stored under $scope.account and/or $scope.contact.
Is this possible or is there a better way to do this?
Share Improve this question asked Dec 29, 2014 at 22:28 JustinJustin 1371 gold badge3 silver badges10 bronze badges 2- This totally works, although it may not be best practice to use this method, in our SPA this is what we need. I don't know hot to mark this as an answer since this is a ment. – Justin Commented Dec 30, 2014 at 16:33
- I posted a quick answer - was on mobile at the time when I mented. – tymeJV Commented Dec 30, 2014 at 16:40
2 Answers
Reset to default 7Since $scope
is just an object with properties, you can use bracket notation:
$scope[partialtype];
While this seems like it is possible, I would suggest creating a custom Angular service that encapsulates your logic for http requests. You could then include your service in your controller, and access the functions in your service.
Take a look at the Angular documentation for creating custom services here: Angular Documentation for Services
本文标签: javascriptAngularJS pass a scope variable name as a function parameterStack Overflow
版权声明:本文标题:javascript - AngularJS pass a scope variable name as a function parameter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742274166a2444875.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论