【发布时间】:2016-03-09 06:55:11
【问题描述】:
这是我的代码
$http.get("/Student/GetStudentById?studentId=" + $scope.studentId + "&collegeId=" + $scope.collegeId)
.then(function (result) {
});
在上面的代码中,使用 http 服务根据 id 获取学生详细信息。但我想像在 c#.net 中一样编写上述服务 string.format
(eg:- string.format("/Student/GetStudentById/{0}/collegeId/{1}",studentId,collegeId)
【问题讨论】:
-
但你不能 :) 从 ECMAScript 6 开始有一个解决方案,在这里查看stackoverflow.com/questions/3304014/…
-
不要忘记,除了最琐碎的情况外,这不足以构成一个 URL。您可能需要使用
encodeURIComponent对这些替换进行URL 转义。在您的特定情况下,从{studentId: $scope.studentId, collegeId: $scope.collegeId}这样的 JavaScript 对象到正确编码的一系列查询参数的转换器是最好的计划。 -
我真的不认为字符串格式实际上会对你的代码产生任何影响,除了改进代码格式。所以我建议你在调用 $http.get(.. )
-
你可以像这样使用字符串替换函数:'/Student/GetStudentById?studentId={studentId} &collegeId= {collegeId}'.replace('{studentId}',$scope.studentId)。替换('{collegeId}', $scope.collegeId)
标签: javascript angularjs