简体   繁体   English

将对象传递给动态生成的html中的javascript函数

[英]Pass object to javascript function in dynamically generated html

I have run into a problem along these lines a couple of times now and rather than create a 'hack' work around I thought it best to ask is there a better way to handle a situation like this. 我已经在这方面遇到了几次问题,而不是绕开我的工作,我认为最好问问是否有更好的方法来处理这种情况。

I am using angular datatables and on the datatable we have a cell with a clickable id which brings up a bootstrap modal. 我正在使用角度数据表,在该数据表上,我们有一个带有可点击ID的单元格,它会显示一个引导程序模态。

I would like to pass in the full object of the item in the row on an ng-click action however it doesn't seem to work and I think it's caused by the object being awkwardly passed into the function which is some dynamically generated html: 我想在ng-click动作中传递该行中项目的完整对象,但是它似乎不起作用,我认为这是由于该对象笨拙地传递给该函数而引起的,该函数是一些动态生成的html:

Datatable: 数据表:

$scope.dtColumns = [DTColumnBuilder.newColumn('Id').withTitle('Id').renderWith(CommonService.renderId),
                            DTColumnBuilder.newColumn('status').withTitle('Status').renderWith(CommonService.renderStatus),
                            DTColumnBuilder.newColumn('description').withTitle('Description').renderWith(CommonService.renderLength),
                            DTColumnBuilder.newColumn('rating').withTitle('Rating').withOption('type', 'rating'),
                            DTColumnBuilder.newColumn('date').withTitle('Date'),
                            DTColumnBuilder.newColumn("category").withTitle('Category').renderWith(CommonService.renderLength),
                            DTColumnBuilder.newColumn("subcategory").withTitle('Sub Category').renderWith(CommonService.renderLength)];

CommonService: CommonService:

function renderId(data, type, full, meta){

        var render = "ID-"+data;

        //'hack' function to store object for later lookup
        setObjectForModal(full);

        //Dynamically generated html to apply link on id with ng-click function which passes in 'full' object

        var html = ' <span class="link" ng-click="showModal('+full+')" > ' + render + ' </span> ';

        return html;
    }

As you can see showModal('+full+') i believe appending the object this way is rendering it strangely in the html it displays as showModal([Object object]) is there any way to get this to work as currently clicking on this link does nothing, but passing in the id as an int raises the modal fine. 如您所见,showModal('+ full +')我相信以这种方式附加对象会在显示为showModal([Object object])的html中奇怪地渲染它,因此有任何方法可以使它正常工作,因为当前单击此链接什么都不做,但是将id传递为int会提高模态罚款。

As a further similar experience I also noticed this same issue in my protractor testing. 作为进一步的类似经验,我在量角器测试中也注意到了相同的问题。 Saying: 他说:

expect('ID-'+id).toEqual(ID-123456);

produces an error along the lines of ID-[Object object] is not equal to ID-123456. 沿着ID- [Object object]不等于ID-123456产生错误。

Is there some string concatenation issue I'm causing and is there a way to prevent it? 我是否引起某些字符串连接问题,并且有办法防止它发生?

Thanks! 谢谢!

By pure chance I found the solution to this problem. 碰巧的是,我找到了解决该问题的方法。 Basically comes down to dynamic html rendering, to pass an object like above you must use JSON.Stringify() to convert to [Object object] into a json string which you can read the properties off. 基本上归结为动态html渲染,要传递上述对象,您必须使用JSON.Stringify()将[Object object]转换为json字符串,您可以读取属性。

function renderId(data, type, full, meta){

    var render = "ID-"+data;

    //'hack' function to store object for later lookup
    setObjectForModal(full);

    //Dynamically generated html to apply link on id with ng-click function which passes in 'full' object

    var html = ' <span class="link" ng-click="showModal('+ JSON.stringify(full) +')" > ' + render + ' </span> ';

    return html;
}

Hope this helps someone. 希望这对某人有帮助。

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

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