Angular js如何在最后一次替换文本" /"在url中(在textarea中)

时间:2014-11-25 17:58:42

标签: jquery angularjs performance

我需要帮助来取代" 123"在这个网址" http://example.com/xyz/123" " 123"价值会有所不同。

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

提前谢谢。

2 个答案:

答案 0 :(得分: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.

});

答案 1 :(得分:0)

它不是一个真正的角度问题,可以通过简单的确定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