Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
156
rated 0 times [  159] [ 3]  / answers: 1 / hits: 63342  / 9 Years ago, fri, march 13, 2015, 12:00:00

In my AngularJS application, I have an issue with string replace in HTML.



Expectation:



Using the same variable as section title and partial of button's label.




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


Scenario:



I have headerText $scope variable. It contains the LabelNames of each section:



$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.,)'
}];


This LabelName should be the title for each section and the same LabelName need to be used for the button's label text along with the text Attach and also need to remove the text in between the brackets.



So in the HTML file, I tried the below code to achieve the result:



<div ng-repeat=header in headerText>
<span ng-bind=header.LabelName></span>
<br />
<button>{{addText.replace({0}, header.LabelName).replace(/((.*))/g, '')}}</button>
<hr />
</div>


Mean, I want to replace the content with brackets along with empty space

(Form (13G), Form (12C) and etc.,)

from

Submitted Forms (Form (13G), Form (12C) and etc.,)

and to use that in the button's label text.



I tried the regexp .replace(/((.*))/g, ''), but it is not supporting.



Is there any way to achieve this in HTML itself.



Sample Plunker


More From » regex

 Answers
82

Move the javascript to script.js and return the value





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, '')
};

}

<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>




[#67446] Wednesday, March 11, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alorac

Total Points: 262
Total Questions: 82
Total Answers: 97

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
alorac questions
Sat, Oct 10, 20, 00:00, 4 Years ago
Tue, Sep 22, 20, 00:00, 4 Years ago
Wed, Jul 1, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Sun, May 17, 20, 00:00, 4 Years ago
;