我想将html字符串中的字符{
和}
替换为{{
和}}
我正在使用替换函数,但我有以下错误:< / p>
$scope.selectedtemplate.template.replace is not a function
我的代码
$scope.selectedtemplate.template = $scope.selectedtemplate.template.replace("{" , "{{" ) ;
$scope.selectedtemplate.template = $scope.selectedtemplate.template.replace("}" , "}}" ) ;
template
是HTML字符串
答案 0 :(得分:3)
以下是一个示例:http://fiddle.jshell.net/33tom882/2/
您可能会收到“替换不是函数”错误,因为 $ scope.selectedtemplate.template 未定义或不是类型的字符串。有关string.replace函数的更多信息,请参见此处:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
似乎有更好的方法可以解决您的实际问题,即使用Angular $ scope属性来管理变量,而不是直接将HTML修改为字符串。