如何在angularjs中编写字符串格式,就像c#一样?

这个问题在这里已经有了答案:

  • 等同于jQuery中的String.format 20个答案

  •    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) {
          });
    

    尝试这个,

    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)
    

    你可以使用JavaScript的sprintf()。

    请看看sprintf()

    链接地址: http://www.djcxy.com/p/90577.html

    上一篇: How to Write string format in angularjs like as c#?

    下一篇: sql server