Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.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
如何获取元素ViewChild angular2和jquery的ID?_Jquery_Angular - Fatal编程技术网

如何获取元素ViewChild angular2和jquery的ID?

如何获取元素ViewChild angular2和jquery的ID?,jquery,angular,Jquery,Angular,我需要在angular2的jquery和ViewChild之间建立一个关系 我尝试了这个,但得到了未定义的值 @ViewChild('item', {read: ViewContainerRef}) Item; console.log( $(this.Item.nativeElement).attr('id')) ); 更新部分 <div class="container col411 col-sm-4" id="div411Item" style="border:

我需要在angular2的jquery和ViewChild之间建立一个关系

我尝试了这个,但得到了未定义的值

@ViewChild('item', {read: ViewContainerRef}) Item;

console.log( $(this.Item.nativeElement).attr('id')) );
更新部分

<div class="container col411 col-sm-4" id="div411Item" 
        style="border: 1px solid #9DA0A4;" (dblclick)="ActiveEditcol411()" #Item>
    new content
</div>

ActiveEditcol411(){
    $('#Item').prop('contenteditable','true');
}

新内容
ActiveEditcol411(){
$('#Item').prop('contenteditable','true');
}
  • 代码中有一个额外的括号

    console.log($(this.Item.nativeElement.attr('id')

  • 你确定你的html标签中有“id”属性吗?如果没有,请添加它并尝试以下代码: console.log($(this.Item.nativeElement.attr('id'))

  • 最新答复:

    <div class="container col411 col-sm-4" id="div411Item"  style="border: 1px solid #9DA0A4;" (dblclick)="ActiveEditcol411()" #Item>
    new content
    </div>
    
    @ViewChild('Item', {read: ViewContainerRef}) Item;
    
    ActiveEditcol411(){
        $(this.Item.element.nativeElement).prop('contenteditable','true');
    
    }
    
    
    新内容
    @ViewChild('Item',{read:ViewContainerRef})项;
    ActiveEditcol411(){
    $(this.Item.element.nativeElement).prop('contenteditable','true');
    }
    
  • 代码中有一个额外的括号

    console.log($(this.Item.nativeElement.attr('id')

  • 你确定你的html标签中有“id”属性吗?如果没有,请添加它并尝试以下代码: console.log($(this.Item.nativeElement.attr('id'))

  • 最新答复:

    <div class="container col411 col-sm-4" id="div411Item"  style="border: 1px solid #9DA0A4;" (dblclick)="ActiveEditcol411()" #Item>
    new content
    </div>
    
    @ViewChild('Item', {read: ViewContainerRef}) Item;
    
    ActiveEditcol411(){
        $(this.Item.element.nativeElement).prop('contenteditable','true');
    
    }
    
    
    新内容
    @ViewChild('Item',{read:ViewContainerRef})项;
    ActiveEditcol411(){
    $(this.Item.element.nativeElement).prop('contenteditable','true');
    }
    
    @khalil_diouri在此处添加更多源代码或创建一个plunker。您在哪里调用console.log()?我在其他组件中动态添加了一个组件,当我添加此组件两次时,我在主组件中有一些元素,其中一些ID值是相同的内容。一些如何避免这种情况(因为我可以通过jquery简单地获取ID)这是因为我试图使用ViewChild////-------------------------------我在dbclick事件后调用console.log!当我在console.log中显示这个(this.Item.nativeElement)时,我被取消了定义,我不明白为什么!@ViewChild('Item',{read:ViewContainerRef})项中存在错误;初始化ViewChild时,您应该通过它的区域名称调用它:Item,而不是Item。我运行了我的代码,对于您发布的代码摘录,它工作正常。我不知道为什么它不适合你。也许你还有其他有影响力的代码。您最好创建一个plunker,在上下文中向人们展示您的问题。@khalil_diouri在此处添加更多源代码或创建一个plunker。您在哪里调用console.log()?我在其他组件中动态添加了一个组件,当我添加此组件两次时,我在主组件中有一些元素,其中一些ID值是相同的内容。一些如何避免这种情况(因为我可以通过jquery简单地获取ID)这是因为我试图使用ViewChild////-------------------------------我在dbclick事件后调用console.log!当我在console.log中显示这个(this.Item.nativeElement)时,我被取消了定义,我不明白为什么!@ViewChild('Item',{read:ViewContainerRef})项中存在错误;初始化ViewChild时,您应该通过它的区域名称调用它:Item,而不是Item。我运行了我的代码,对于您发布的代码摘录,它工作正常。我不知道为什么它不适合你。也许你还有其他有影响力的代码。你最好创建一个plunker,让人们了解你的问题所在。