【问题标题】:Angular js how to replace text after last "/" in url (in textarea) [closed]Angular js如何在url中的最后一个“/”之后替换文本(在textarea中)[关闭]
【发布时间】:2014-11-25 17:58:41
【问题描述】:

我需要帮助来替换此网址中的“123”“http://example.com/xyz/123”“123”值会有所不同。

1) 我的模型带有带有 URL 作为选项的下拉菜单,选定的 URL 将显示在文本区域中。 2) 文本区域有一些我们从下拉列表中选择的文本和 URL。 3)我的问题是:我提供了一些文本并从下拉列表中选择了 URL。然后我从下拉列表中选择另一个 URL,这应该替换第一个 URL 而不会更改文本。

提前谢谢你。

【问题讨论】:

  • 请提供 html 和你的代码 sn-p,例如在 jsfiddle 中

标签: jquery angularjs performance


【解决方案1】:

不确定这是一个角度问题还是通用 JavaScript 问题。由于您没有提供代码来操作,我将尝试以通用方式回答:)

在您的模型上放置一个观察者,该模型具有下拉选择,在下拉列表的父范围内 - 如果您没有创建任何指令并且只是使用 ng-options 来填充您的下拉列表,这将是您的控制器范围。

$scope.url      - selected url 

$scope.$watch('url', function(newVal, oldVal){

     // make search-replace your preferred way ( regex, indexOf-splice )
     // on your textarea model.

});

【讨论】:

    【解决方案2】:

    这不是一个真正的角度问题,可以通过简单确定的 javascript 来完成:

    // at some point of your code your var got this value
    $scope.url = "http://example.com/xyz/123";
    
    //and at another point you want to replace the value after the last '/'
    var foo = $scope.url.split('/');
    foo.pop(); // remove the last item
    foo.push('new'); // add a different one
    $scope.url = foo.join('/'); //join the string
    

    // $scope.url 现在是“http://example.com/xyz/new

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多