Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在内部打印超链接<;p>;数据绑定AngularJS时的标记_Javascript_Html_Angularjs - Fatal编程技术网

Javascript 如何在内部打印超链接<;p>;数据绑定AngularJS时的标记

Javascript 如何在内部打印超链接<;p>;数据绑定AngularJS时的标记,javascript,html,angularjs,Javascript,Html,Angularjs,我正在使用一个控制器来转换标记中的任何web链接,并在标记中显示它们。我希望它以超链接格式显示链接,如中所示 很好,但它显示了 这是我的密码 我的HTML <div class="col-sm-10 col-sm-offset-1"> <div class="row" style=" margin-top:70px;"> <div id='dv1'> <form>

我正在使用一个控制器来转换
标记中的任何web链接,并在
标记中显示它们。我希望它以超链接格式显示链接,如中所示 很好,但它显示了

这是我的密码

我的HTML

    <div class="col-sm-10 col-sm-offset-1">
    <div class="row" style=" margin-top:70px;">

    <div id='dv1'>
             <form> 

                <input ng-model="comment.txtcomment" id="txtcomment" style='width:500px' >
                <button ng-click="addComment()" style='margin-top:10px;'>Post Comment</button>
             </form> 

            <!--displaying comments-->
            <h4>Comments</h4>
                  <p>{{myText}}</p>

    </div>
    <div></div>

发表评论
评论
{{myText}}

我的控制器

(function(){
    angular.module('StayFit')
    .controller('CommentController',['$scope','$state','$http', function($scope,$state,$http){
        $scope.user=JSON.parse(localStorage['User-Data']) || undefined;
        console.log( $scope.user.data);
        $scope.comment={};

        $scope.addComment=function(){
      var text=$scope.comment.txtcomment;  

      //this code will identify any links  
      var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
      var text1=text.replace(exp, "<a href='$1'>$1</a>");
      var exp2 =/(^|[^\/])(www\.[\S]+(\b|$))/gim;
      var val=text1.replace(exp2, '$1<a target="_blank" href="http://$2">$2</a>');
      $scope.myText=val;

        }//.error(function(error){console.log(error);});
    }])}());



      Tried using ng-binding-html, also doesn't work
(函数(){
角度模块('StayFit')
.controller('CommentController',['$scope','$state','$http',函数($scope,$state,$http){
$scope.user=JSON.parse(localStorage['user-Data'])| |未定义;
log($scope.user.data);
$scope.comment={};
$scope.addComment=function(){
var text=$scope.comment.txtcoment;
//此代码将标识任何链接
var exp=/(\b(https?| ftp |文件):\/\/[-A-Z0-9+&@#\/%?=~~|!:,.;]*[-A-Z0-9+&@#\/%=~|]/ig;
var text1=text.replace(exp,“”);
var exp2=/(^.[^\/])(www\.[\S]+(\b\$)/gim;
var val=text1.replace(exp2,$1');
$scope.myText=val;
}//.error(函数(错误){console.log(错误);});
}])}());
尝试使用ng绑定html,也不起作用

您显然希望将文本用作HTML,而不是文本,因此需要使用


请注意这一点,因此您必须使用类似于
$scope.myTextVal=$sce.trustAsHtml(val)的东西

将其更改为这样的

给出了以下错误:[$sce:unsafe]试图在安全上下文中使用不安全值。现在p标签没有值了,您还需要使用$sce服务。我已经更新了我的答案。