HTML中的AngularJS字符串替换 [英] AngularJS string replace in HTML

查看:80
本文介绍了HTML中的AngularJS字符串替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 AngularJS 应用程序中,我在HTML中遇到字符串替换问题。

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

期望:

使用相同的变量作为节标题和部分按钮的标签。

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

情景:

I有headerText $ scope 变量。它包含每个部分的 LabelName

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

这个 LabelName 应该是每个的标题部分和相同的 LabelName 需要用于按钮的标签文本以及文本附加,还需要删除括号之间的文字。

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.

所以在HTML文件中,我尝试使用以下代码来实现结果:

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>

意思是,我想用括号替换内容以及空格

(表格(13G),表格(12C)等),

来自

提交的表格(表格(13G),表格( 12C)等等,)

并在按钮的标签文本中使用它。

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.

我试过regexp .replace(/(\(。* \))/ g,''),但它不支持。

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

有没有办法在 HTML 本身实现这一点。

Is there any way to achieve this in HTML itself.

Sample Plunker

推荐答案

将javascript移动到script.js并返回值

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>

这篇关于HTML中的AngularJS字符串替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆