如何在控制器中获取 Emberjs 生成的元素 ID [英] How to get Emberjs generated element id in controller

查看:22
本文介绍了如何在控制器中获取 Emberjs 生成的元素 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取一个元素的 Ember 生成的 id.

{{#each}}<div><h1>{{MyComponent}}</h1>

{{/每个}}

呈现如下内容:

<h1>我的组件在这里</h1>

我需要获取控制器内每个 div 元素的 id.如果我必须使用自定义 id,那么如何使用 ember 视图创建自定义 id.

我如何通过控制器内部的 id 来识别每个 div 元素?

解决方案

我不太清楚你的意思,但我可以回答:

<块引用>

我如何通过控制器内部的 id 来识别每个 div 元素?

工作示例 查看 console.log 以查看element id,然后使用 Google Chrome 或 Firebug 检查组件元素以查看 id 是否相同.

App.ItemOneComponent = Ember.Component.extend({didInsertElement: 函数(){console.log('元素 id: ' + this.elementId);}});

组件内部的

this就是组件,elementId获取的是div的id(或者不同的标签,如果你在里面使用tagName零件).

如果要使用 JQuery 选择组件,请在组件内部使用:this.$().

现在我不知道您需要什么,所以我将提出一些建议.如果您需要知道控制器中每个组件的 id,我建议将位于控制器上的数组传递给组件.在您的 didInsertElement 中,将 elementIdjQuery 对象添加到数组中.这就是我的意思

App.IndexController = Ember.ArrayController.extend({组件 ID:[]});App.ItemOneComponent = Ember.Component.extend({didInsertElement: 函数(){this.get('array').pushObject(this.elementId);}});

并且您的组件将属性传入:{{item-one array=controller.componentIds}}

您还可以通过带有 this.sendAction('actionName', componentId) 的操作发送组件的 id 或 jQuery 对象,并在您的控制器上定义一个操作接受一个参数(即 componentId 或 jQuery 对象.

I would like to get the Ember generated id of an element.

{{#each}}
<div>
 <h1>{{MyComponent}}</h1>
</div>
{{/each}}

which render something like:

<div id="ember180" class="ember-view">
 <h1>My Component here</h1>
</div>

I need to get the id of each div element inside a controller. If I've to use custom id then how can I create custom id using ember view.

And how can I identify each div element by its id inside controller?

解决方案

I'm not sure exactly what you mean, but I can answer:

And how can I identify each div element by its id inside controller?

Working Example Look at the console.log to see the element id, and then use Google Chrome or Firebug to inspect the component element to see that the id is the same.

App.ItemOneComponent = Ember.Component.extend({
  didInsertElement: function(){
    console.log('element id: ' + this.elementId);
  }
});

this inside the component is the component, and elementId gets the id of the div (or different tag if you use tagName inside the component).

If you want to select the component with JQuery, use: this.$() inside of the component.

Now here is where I don't know what you need so I'm going to throw out a few suggestions. If you need to know the ids of every component from the controller, I would recommend passing in an array sitting on the controller to the component. In your didInsertElement, add the elementId or the jQuery object to the array.Here is what I mean

App.IndexController = Ember.ArrayController.extend({
  componentIds: []  
});

App.ItemOneComponent = Ember.Component.extend({
  didInsertElement: function(){
    this.get('array').pushObject(this.elementId);
  }
});

And your component passes the property in: {{item-one array=controller.componentIds}}

You could also send the component's id or jQuery object via an action with this.sendAction('actionName', componentId) and have an action defined on your controller that takes one parameter (ie the componentId or the jQuery object.

这篇关于如何在控制器中获取 Emberjs 生成的元素 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆