我需要在对象的属性值中替换字符。我正在遍历对象数组(输出到控制台)并检索FeatureUrl属性。

我以以下形式从Svc返回该属性的数据:

index.html#/ blah

我需要将“#”替换为“#/ app”,以便新的网址以以下形式返回:

index.html#/ app / blah

我不确定.replace是否是在这里使用的正确方法,但这是我所看到的建议。有人可以指出我正确的方向吗?

var localFeatureDetails = function() {
    $scope.user = userService.GetUserInformation();
    $scope.featureDetails = $scope.user.Features;

  var featureUrlRewrite = function () {
    var index;
    var urlCount;
    for (index = 0; index < $scope.featureDetails.length; index++) {
      urlCount = $scope.featureDetails[index];
      urlCount.FeatureUrl.replace("#","#/app");
      console.log(urlCount);
    }
  };

  featureUrlRewrite();

};
localFeatureDetails();

最佳答案

我没有测试您的代码,但是基于.replace()的工作方式,您必须通过覆盖它来再次将值分配给对象属性,否则就不会保存该值。

假设其他一切正确,请尝试以下操作:
$scope.featureDetails[index].FeatureUrl = urlCount.FeatureUrl.replace("#","#/app");

参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

09-20 21:20