【问题标题】:How to Write string format in angularjs like as c#? [duplicate]如何像 c# 一样在 angularjs 中编写字符串格式? [复制]
【发布时间】: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


【解决方案1】:
   String.format = function () {
      // The string containing the format items (e.g. "{0}")
      // will and always has to be the first argument.
      var theString = arguments[0];

      // start with the second argument (i = 1)
      for (var i = 1; i < arguments.length; i++) {
          // "gm" = RegEx options for Global search (more than one instance)
          // and for Multiline search
          var regEx = new RegExp("\\{" + (i - 1) + "\\}", "gm");
          theString = theString.replace(regEx, arguments[i]);
      }

      return theString;
  }

  $http.get(String.format("/Student/GetStudentById?studentId={0}&collegeId={1}", $scope.studentId , $scope.collegeId))
      .then(function (result) {
      });

【讨论】:

    【解决方案2】:

    试试这个,

    String.format = function(str) {
       var args = arguments;
       return str.replace(/{[0-9]}/g, (matched) => args[parseInt(matched.replace(/[{}]/g, ''))+1]);
    };
    
    string.format("/Student/GetStudentById/{0}/collegeId/{1}",studentId,collegeId)
    

    【讨论】:

      【解决方案3】:

      你可以使用javascript的sprintf()。

      请看sprintf()

      【讨论】:

        【解决方案4】:

        这是Rest parameter 在 ES6 中派上用场的地方。而且,这是 String.Format 在 C# 中的另一种 JS 替代方案。

        String.prototype.format = function(...args) {
            let result = this.toString();
            let i = 0;
            for (let arg of args) {
                let strToReplace = "{" + i++ + "}";
                result = result.replace(strToReplace, (arg || ''));
            }
            return result;
        }
        

        例如

        var path = "/Student/GetStudentById/{0}/collegeId/{1}";
        var studentId = "5";
        var collegeId = "10";
        var result = path.format(studentId, collegeId);
        console.log(result);
        

        这个输出,

        /Student/GetStudentById/5/collegeId/10

        【讨论】:

          猜你喜欢
          • 2021-04-23
          • 1970-01-01
          • 2017-04-20
          • 1970-01-01
          • 2018-09-02
          • 2018-08-10
          • 1970-01-01
          • 2023-03-15
          • 2021-06-17
          相关资源
          最近更新 更多