Aru*_*mar 9 javascript regex angularjs
在我的AngularJS应用程序中,我遇到了HTML中的字符串替换问题.
期望:
使用相同的变量作为节标题和部分按钮的标签.
Submitted Forms (Form (13G), Form (12C) and etc.,) Attach Submitted Forms Planned Plans (Plan (13G), Plan (12C) and etc.,) Attach Planned Plans Defined Schemes (Scheme (13G), Scheme (12C) and etc.,) Attach Defined Schemes Paid Insurances (Insurance (13G), Insurance (12C) and etc.,) Attach Paid Insurances
场景:
我有headerText $scope变量.它包含LabelName每个部分的s:
$scope.headerText = [{
LabelName: 'Submitted Forms (Form (13G), Form (12C) and etc.,)'
}, {
LabelName: 'Planned Plans (Plan (16K), Plan (12C) and etc.,)'
}, {
LabelName: 'Defined Schemes (Scheme (11H), Scheme (12C) and etc.,)'
}, {
LabelName: 'Paid Insurances (Insurance (10G), Insurance (12C) and etc.,)'
}];
Run Code Online (Sandbox Code Playgroud)
这LabelName应该是每个部分的标题,同样LabelName需要将按钮的标签文本与文本一起使用Attach,还需要删除括号之间的文本.
所以在HTML文件中,我尝试了以下代码来实现结果:
<div ng-repeat="header in headerText">
<span ng-bind="header.LabelName"></span>
<br />
<button>{{addText.replace("{0}", header.LabelName).replace(/(\(.*\))/g, '')}}</button>
<hr />
</div>
Run Code Online (Sandbox Code Playgroud)
意思是,我想用括号替换内容以及空白空间
(表格(13G),表格(12C)等),
从
提交的表格(表格(13G),表格(12C)等),
并使用在按钮的标签文本中.
我尝试了正则表达式.replace(/(\(.*\))/g, ''),但它不支持.
有没有办法实现这一点HTML.
anp*_*smn 10
将javascript移动到script.js并返回值
angular.module('app', []);
function StringCtrl($scope) {
$scope.headerText = [{
LabelName: 'Submitted Forms (Form (13G), Form (12C) and etc.,)'
}, {
LabelName: 'Planned Plans (Plan (13G), Plan (12C) and etc.,)'
}, {
LabelName: 'Defined Schemes (Scheme (13G), Scheme (12C) and etc.,)'
}, {
LabelName: 'Paid Insurances (Insurance (13G), Insurance (12C) and etc.,)'
}];
$scope.addText = 'Attach {0}';
$scope.getText = function(obj){
return $scope.addText.replace("{0}", obj).replace(/(\(.*\))/g, '')
};
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app" ng-controller="StringCtrl">
<div ng-repeat="header in headerText">
<span ng-bind="header.LabelName"></span>
<br />
<button ng-bind="getText(header.LabelName)"></button>
<hr />
</div>
</body>Run Code Online (Sandbox Code Playgroud)
为此目的创建一个方法会好得多:
var addText = 'Attach {0}';
$scope.getButtonLabel = function(label){
return addText.replace('{0}', label.substr(0,label.indexOf("(")));
};
Run Code Online (Sandbox Code Playgroud)
然后在您的标记中使用它:
<button>{{getButtonLabel(header.LabelName)}}</button>
Run Code Online (Sandbox Code Playgroud)