【问题标题】:Ember _ How to access to a controller action from a component controllerEmber _ 如何从组件控制器访问控制器操作
【发布时间】:2017-10-24 20:49:54
【问题描述】:

我正在尝试向 ember 组件添加一些外部功能。组件如下:

应用/模板/programmers.hbs

{{echo-hlabel-tf 
   id= "id-test-hlabel"
   class="class-test-hlabel-mio"
   label="Horizontal textfield"
   textValue="..."
   regExp="^([a-z0-9]{5})$"
   errorMsg="Text hasn't five characters"
   fnActionButtonClicked = "sayHello"
}}

我正在使用参数 'fnActionButtonClicked' 。此参数表示它执行的函数,它不在组件的功能范围内(例如将 javascript 函数作为参数传递)。我是新手,我不知道确切的 ember 方法。组件的模板是:

应用程序/模板/组件/echo-hlabel-tf.hbs

<div id="echo-hlabel-tf">
    <span id="{{id}}-echo-hlabel-tf-label" 
          class="{{class}}-echo-hlabel-tf-hlabel">
    {{label}}
</span>
<span id="{{id}}-echo-hlabel-tf-value" 
      class="{{class}}-echo-hlabel-tf-value">
    {{input id='input-id' class='input-class' value=textValue
            focus-out = (action 'validateRegExp' textValue regExp) 
    }}
</span>
<span id="{{id}}-echo-hlabel-tf-error-msg" 
 class="{{class}}-echo-hlabel-tf-error-msg">
    <p id='error-msg' class="error-msg">
        {{errorMsg}}
    </p>
</span>

<div>
    <h2>Testing passing functions to the component</h2>
        <input type="button" {{action "actionButtonClicked"}}/>
</div>
</div>

如您所见,在模板的底部,有一个 input type="button" 与 "actionButtonClicked" 绑定。组件的控制器是:

应用/组件/echo-hlabel-tf.js

import Ember from 'ember';

export default Ember.Component.extend({
   classNameBindings:['error'],
   error:false,

   actions: {
    validateRegExp(text,regExpStr,event){
        let regExpObj = new RegExp(regExpStr)
        if(regExpObj.test(text)){
            this.set('error',false);
        }else{
            this.set('error',true);
        }
    },
    actionButtonClicked(){
        console.log('Arrives to the component');
        var action = this.get('fnActionButtonClicked');
        console.log(action);
        // How can I call the function in another controller
        // With parameter fnActionButtonClicked name
    }
 }
});

所以,模板'fnActionButtonClicked'(SayHello)的参数会在actionButtonClicked()中通过var action = this.get('fnActionButtonClicked '); .

所以,在这一点上,出现了疑问。由于我不能将 javascript 函数作为模板的参数传递(或者至少,我认为这不是正确的做法),我创建了一个控制器 'sampleController' 来创建使用以下代码操作 'sayHello'(参见参数 fnActionButtonClicked 值):

app/controllers/sample-controller.js

从“ember”导入 Ember;

export default Ember.Controller.extend({
   actions:{
    sayHello(){
        console.log("I'm saying hello this time");
    }
  }

});

我怎样才能从 app/components/echo-hlabel-tf.js:actionButtonClicked() 执行 app/controls/sample-controller.js 的代码:sayHello() ?如何从组件控制器访问另一个控制器?这是实现我目标的正确方法吗?

提前致谢

【问题讨论】:

    标签: javascript ember.js ember-components ember-controllers


    【解决方案1】:

    您需要为特定路由 programmers 创建控制器,而不是 sample-controller.js。所以创建app / controllers / programmers.js文件,

    export default Ember.Controller.extend({
      actions:{
        sayHello(){
          console.log("I'm saying hello this time");
        }
      }
    })
    

    在组件内部app / components / echo-hlabel-tf.js

    actionButtonClicked(){
      console.log('Arrives to the component');
      this.sendAction('fnActionButtonClicked');        
    }
    

    【讨论】:

    • 好像还是不行。我已经在 app/controllers/programmers.js 中创建了控制器,并按照您所说的发送了操作,但我得到了“ ember.debug.js:18008 没有处理“sayHello”操作。如果您确实处理了该操作,则此错误可能是由于从控制器中的操作处理程序返回 true 导致该操作冒泡。" .似乎没有找到方法:(
    • 你是不是在programmers.hbs模板中包含了组件echo-hlabel-tf.hbs
    • 看看我为你创建的this twiddle link
    • 感谢您的旋转。我正在看它。非常感谢jeje
    • 对不起对不起对不起...我敢肯定,在那次jajaja之后你会恨我...你从一开始就完全正确。只是,我创建了一个名为 programmer.hbs 的控制器,而不是 programmers.hbs jejeje ......这真的很奇怪。抱歉让您浪费时间,再次感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多