angular.js - How does angular call the trigger method for new nodes?
phpcn_u1582
phpcn_u1582 2017-05-15 17:08:56
0
2
533

How to call the triggerHandler method for dynamically added nodes in Angular?

My nodes are dynamically generated based on a table:

 <a class="btn btn-default J_Local" href="javascript:void(0);" id="J_MapImg_{{my.sku_val_id}}"  ng-click="uplod($event)"><span>本地上传</span></a>

The click event is as follows:

$scope.uplod = function(obj){
    supload.init({
        button: ''+obj.currentTarget.id+'', uploaded: function (uploader, file, response) {
            if (response.code == 1){
                layer.msg(response.msg);
            }
            else {
                $('#'+obj.currentTarget.id).siblings("p.preview").html("<img src='" + response.src + "' width='20' height='20'/>");
            }
        }
    });
    $('#'+obj.currentTarget.id+'').off("click");
}

But the dynamically generated image calls my image upload and requires double-clicking to be punished. I want to have it automatically trigger a click when a click is generated. How to achieve this?

phpcn_u1582
phpcn_u1582

reply all(2)
漂亮男人

If it isangularjs, it is wrong to mix jQuery in it
For your scenario, there is no need to dynamically insert html code, and master the following knowledge points

angular.module('app', [])
.filter("trustUrl", ['$sce', function ($sce) {
    return function (recordingUrl) {
        return $sce.trustAsResourceUrl(recordingUrl);
    };
}])
function($scope, $ajax) {
    $scope.src = null;
    
    $scope.upload = function() {
        $ajax.post(...., function(response){
            $scope.src = response.src;
            $scope.$apply(); //通知更改了
        }
        
    }
}


<img src="{{src || 'http://...../upload.jpg'|trustUrl}}" ng-click="upload()">
淡淡烟草味

If it is jquery, you need to use the $().on("click",function(){}) form

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template