简体   繁体   English

如何将回调从父组件传递到嵌套的路由组件?

[英]How to pass down a callback from a parent component to a nested routed component?

I have the following format where I have a global component that has 3 nested components that are activated based on a given route: 我有以下格式,其中我有一个全局组件,该组件具有3个嵌套组件,这些嵌套组件根据给定的路线被激活:

  $stateProvider
    .state('create-goal', {
      url: '/create-goal',
      component: 'createGoal',
      redirectTo: 'create-goal.step-1'
    })
    .state('create-goal.step-1', {
      url: '/step-1',
      component: 'step1'
    })
    .state('create-goal.step-2', {
      url: '/step-2',
      component: 'step2'
    })
    .state('create-goal.step-3', {
      url: '/step-3',
      component: 'step3'
    });

Inside of the main create-goal html file, I have the following: 在主要的create-goal html文件中,我具有以下内容:

  <ui-view goal="$ctrl.goal"
           goalInfo="$ctrl.goalInfo"
           processStep1="$ctrl.processStep1">
  </ui-view>

The goal and goalInfo work great as they are data that is one way data bound. goalgoalInfo工作,因为它们是数据的一种数据绑定方式。 However, when I want to pass down a function, such as processStep1 to compute some action on step-1 and so forth, that function does not show up in the step-1 component even though the goal and goalInfo do. 但是,当我想传递诸如processStep1类的功能以计算对step-1某些操作等等时,即使goalgoalInfo都可以在step-1组件中显示该功能。

export default {
  name: 'step1',
  restrict: 'E',
  bindings: {
    goal: '<',
    processStep1: '&'
  },
  template,
  controller
};

Thoughts? 思考?

There a few ways you could go about doing this. 您可以通过几种方法执行此操作。

  • Access the function using $parent (I recommend against this, as it is relative and can cause problems 使用$ parent来访问该函数(我建议不要这样做,因为它是相对的,可能会导致问题
  • Pass it down using attributes to inject it into each child function using the '=function' or '&function' depending on your use case 根据您的使用情况,使用属性将其传递下来,以使用'= function'或'&function'将其注入每个子函数中
  • Set the function in a helper service that can be injected anywhere. 在可以插入到任何地方的帮助服务中设置功能。 This is great if you plan on using this a lot and if it is just for data manipulation it really belongs in a service anyways to avoid bloated controllers. 如果您打算大量使用它,并且如果它只是用于数据操作,则它确实属于服务中,以避免控制器过大,这非常好。
  • The last way would be use models. 最后一种方法是使用模型。 This would be good for if it is a function to be run on a certain type of object like users that can easily be modeled. 如果它是要在可以轻松建模的特定类型的对象(例如用户)上运行的功能,那么这将是一件好事。 Such as a .save or .update function. 例如.save或.update函数。

https://docs.angularjs.org/guide/directive http://fdietz.github.io/recipes-with-angular-js/controllers/sharing-code-between-controllers-using-services.html https://www.sitepoint.com/tidy-angular-controllers-factories-services/ http://www.webdeveasy.com/angularjs-data-model/ https://docs.angularjs.org/guide/directive http://fdietz.github.io/recipes-with-angular-js/controllers/sharing-code-between-controllers-using-services.html https:// www.sitepoint.com/tidy-angular-controllers-factories-services/ http://www.webdeveasy.com/angularjs-data-model/

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

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