简体   繁体   English

使用AngularJS调用函数

[英]Calling a function using AngularJS

I am working on application in AngularJs 我正在研究AngularJs中的应用程序

Here is my code : 这是我的代码:

HTML HTML

<div ng-controller="MainController as main" class="containerWrap">
..
..
..
<p ng-bind-html="parseMsg(msg)"></p>
..
..

The parseMsg function passes the msg filled by the user to the controller JS defined as follows : parseMsg函数将用户填充的msg传递给控制器​​JS,定义如下:

this.parseMsg=function(msg){
...
...
    if(msg['subtype']==FILES_UPLOADING){
        $scope.showUploadOptions=true;
        var per=parseInt(msg['text']);
        if(per==100)
            msg['text']='<div ng-if="showUploadOptions"><img ng-src="logo.png"  ng-click="main.cancelFileUpload(chat,msg_id)"/></div>';
        return $sce.trustAsHtml(msg['text']);
    }
...
...
...
};

Chat and msg_id being passed to the main.cancelFileUpload are stored previously as: Chat和传递给main.cancelFileUpload的msg_id先前存储为:

$scope.chat = (object)
$scope.msg_id =(object) 

this.cancelFileUpload=function(chat,msg_id){
        alert("Cancel clicked ");
        delete this.retryUploadFiles[msg_id];
        delete chat.msgs[msg_id];
    };

What I am trying to do is cancel the file upload being sent during the server request using a click on an image. 我想要做的是取消在服务器请求期间使用单击图像发送的文件上传。

This ng-click is not responding. 此ng-click没有响应。 It doesn't give an error but doesn't function properly either. 它不会给出错误但也无法正常运行。

Can anyone help identify the issues? 任何人都可以帮助确定问题吗?

Thanks! 谢谢!

ngBindHtml don't compile your HTML. ngBindHtml不编译您的HTML。 So, all angular directives from your dynamic HTML(ng-if, ng-src, ng-click, etc..) will not work. 因此,来自动态HTML(ng-if,ng-src,ng-click等等)的所有角度指令都不起作用。

To really compile your html you can use $compile service like that: 要真正编译你的html,你可以使用$ compile服务:

$compile(html)($scope)

But it's not a true way for angular. 但这不是角度的真正方式 If you want to modify DOM structure - you need to use directives. 如果要修改DOM结构 - 需要使用指令。 In your case you can include that html to template itself instead of add it dynamically: 在您的情况下,您可以将html包含到模板本身而不是动态添加它:

<div ng-show="showYourHtml">
    <img ng-src="logo.png"
         ng-click="main.cancelFileUpload(chat,msg_id)"/>
</div>

And your controler: 而你的控制器:

...
if(conditions){
    $scope.showYourHtml = true;
}
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM