在angular js中找到多个###并替换为多个文本框
Find multiple ### and replace with multiple textbox(s) in angular js
我有这样的输入原始数据:
`###` train should I take to get to Kuala `###`, please?
需要将###
以索引方式转换为文本框。
我怎么能做到呢?我期望的输出是:
<input type="text" ng-model="inputTextFIB[index]" class="input-textbox-fill-in-the-blank ng-pristine ng-untouched ng-valid ng-empty"> train should I take to get to Kuala <input type="text" ng-model="inputTextFIB[index]" class="input-textbox-fill-in-the-blank ng-pristine ng-untouched ng-valid ng-empty">, please?
注意:
inputTextFIB[index]
:- 索引创建了多少###
。
谢谢你的帮助
angular
.module('myApp', [])
.run(function($rootScope) {
$rootScope.title = 'myTest Page';
})
.controller('testController', ['$scope', '$sce',
function($scope, $sce) {
var counter = 0;
$scope.myName = '### train should I take to get to Kuala ###, please?';
var count = ($scope.myName.match(/###/g) || []).length;
$scope.myName = $sce.trustAsHtml($scope.myName.replace(/###/g, function(x) {
counter = counter + 1;
return '<input type="text" ng-model="inputTextFIB' + counter + '" class="input-textbox-fill-in-the-blank ng-pristine ng-untouched ng-valid ng-empty">'
}));
$scope.call = function(value) {
alert(value);
}
}
]).directive('compileTemplate', function($compile, $parse) {
return {
link: function(scope, element, attr) {
var parsed = $parse(attr.ngBindHtml);
function getStringValue() {
return (parsed(scope) || '').toString();
}
//Recompile if the template changes
scope.$watch(getStringValue, function() {
$compile(element, null, -9999)(scope); //The -9999 makes it skip directives so that we do not recompile ourselves
});
}
}
});
<!DOCTYPE html>
<html data-ng-app="myApp">
<head>
<link rel="stylesheet" href="style.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body data-ng-controller="testController">
<button ng-click="call(inputTextFIB2)">get</button>
<p ng-bind-html="myName" compile-template></p>
</body>
</html>
多亏了Angular js中$ scene . trustashhtml()字符串中的调用函数。
相关文章:
- JavaScript 在页面加载后替换文本
- 原型JS查找跨度并替换文本
- jQuery在iFrame Src中查找和替换文本
- 用“$$$”替换文本,在Javascript中给出截断的结果
- 如何替换文本区域中的换行符
- 使用 JavaScript 正则表达式替换文本中的类似字符串
- 如何使用 jQuery 替换文本中的 HREF 链接
- :包含不区分大小写的解决方案在替换文本以突出显示时不起作用
- 遍历DOM和替换文本的最佳方法
- Qualtrics(Javascript)-矩阵表-用下拉列表替换文本输入字段
- 使用筛选器替换文本
- JavaScript无法用新文本替换文本
- 替换文本中的随机youtube链接以使用javascript嵌入代码
- 在模态激发后替换文本
- JQuery如何替换文本区域内的字符串
- 可以动态设置图像替换文本吗
- 如何使用javascript在单个正则表达式中递归替换文本字符串中重复的逗号
- 在不更改布局的情况下替换文本的解决方案(jQuery)
- 使用Javascript替换文本区域中的文本
- Imacros替换文本元素