【发布时间】:2014-09-30 09:22:23
【问题描述】:
我开发了一个角度应用程序,我想用具有“日期”类型的输入替换一些关键字,如“[输入日期]”。
我尝试使用正则表达式的替换功能,但不起作用。它向我显示代码而不是输入。
我可以使用 ng-bind-html 显示输入,但我不能使用 ng-model 的值。
<body ng-controller="MainCtrl">
<div ng-bind-html="trustAsHtml(output)"></div>
<p>{{inputValue}}</p>
</body>
var app = angular.module('plunker', ['ngSanitize']);
app.controller('MainCtrl', function($scope, $sce) {
$scope.trustAsHtml = $sce.trustAsHtml;
$scope.foo = 'some text {val}';
var regexValue = new RegExp("{val}", "g");
$scope.output = $scope.foo.replace(regexValue, '<input type="text" name="some_name" ng-model="inputValue" value="">');
});
http://plnkr.co/edit/nmML6X7y6poRvY6eY3K5
编辑
我试试类似的东西
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular-sanitize.js"></script>
</head>
<body ng-app="plunker">
<div ng-controller="MainCtrl">
<p ><date-input model="output" information="info"></date-input></p>
</div>
<script>
var app = angular.module('plunker', []);
app.controller('MainCtrl', ['$scope', function($scope) {
$scope.output = 'Johan';
$scope.info = [{
"id": 0,
"name": "i'm a name",
"type": "text",
"message": "Hello, i'm {{value}} and i love cacao"
}];
}]).directive('dateInput', function() {
return {
restrict: 'E',
scope: {
model: '=model',
information: '=information'
},
template: function(){
var regexValue = new RegExp("{{value}}", "g");
return information.message.replace(regexValue, '<input type="date" ng-model="model">');
}
};
});
</script>
</body>
</html>
但是
错误:找不到变量:信息
【问题讨论】:
标签: javascript angularjs input