【发布时间】:2018-06-09 08:37:01
【问题描述】:
我有一个如下所示的控制器:
(function() {
angular
.module("main")
.controller("HomeCtrl",
["branchResource",
"adalAuthenticationService",
HomeCtrl]);
function HomeCtrl(branchResource, adalService){
var vm = this;
vm.copyrightDate = new Date();
vm.user = adalService.userInfo.userName;
// right here, can I insert the vm.user from above
// as a parameter to the resource's query?
branchResource.query(function (data) {
vm.branches = data;
});
}}());
用户在到达应用程序的这一点时已通过身份验证。所以,用户的信息是可用的。
我有一个后端 API,它接受用户名并返回用户有权访问的分支的名称。我可以将 URL 连同有效的用户名一起粘贴到浏览器中,并获得预期的结果。我正在尝试在我的branchResource 中使用该 API:
(function () {
"use strict";
angular
.module("common.services")
.factory("branchResource",
["$resource", branchResource]);
function branchResource($resource){
return $resource("/api/user/GetAllUserBranches?federatedUserName=:user")
}}());
不过,我的问题是我不知道如何将 vm.user 从控制器传递给 branchResource。有人可以指出我正确的方向吗?
【问题讨论】:
-
查看“用户资源” example in docs。
$resource()中的对象用于告诉它从哪个属性中获取您的:user,您需要将您的userINfo传递给query()
标签: angularjs ngresource