简体   繁体   English

如何将参数从一个组件传递到另一组件

[英]How to pass parameter from one component to another

I have some components, which together form my menu. 我有一些组件,它们一起构成菜单。 I'm wanting to get a parameter in the topmost component, and use it in the lower components. 我想在最上面的组件中获取参数,并在下面的组件中使用它。

I want to pass the 'app-rest' value to my component. 我想将“ app-rest”值传递给我的组件。

angular.module('app').component('application', {
        controller: applicationController,
        template: `
        <cp-layout 
            config="$ctrl.config" 
            menu="$ctrl.menu" 
            clientId="$ctrl.clientId"
            logout="$ctrl.logout()">
            <div ui-view class="content-wrapper ng-scope ng-fadeInRight"></div>
        </cp-layout>
        `
    });

 applicationController.$inject = [ '$window', '$http', 'MensagemService', '$filter', 'Siseg' ];
    function applicationController($window, $http, MensagemService, $filter, Siseg) {
        const vm = this;

        vm.$onInit = function() {
          vm.clientId = 'app-rest';
}

this is cp-layout: 这是cp-layout:

    (function() {
        'use strict';

        angular.module('cp.layout', ['cp.navbar', 'cp.sidebar'])

        .component('cpLayout', {
            bindings : {
                config: '<',
                logout: '&?',
                menu: '<?',
                menuFile: '<?',
                clientId: '@'
            },
            replace: true,
            transclude: true,
            template : `
            <div data-ng-class="{ 'layout-fixed' : $ctrl.config.layout.isFixed, 'aside-collapsed' : $ctrl.config.layout.isCollapsed, 'layout-boxed' : $ctrl.config.layout.isBoxed, 'layout-fs': $ctrl.config.layout.useFullLayout, 'layout-h': $ctrl.config.layout.horizontal, 'aside-float': $ctrl.config.layout.isFloat,'aside-toggled': $ctrl.config.layout.asideToggled}">

   <cp-navbar config="$ctrl.config" logout="$ctrl.logout()"></cp-navbar>

        {{$ctrl.clientId}} / Nothing is printed here.
                <cp-sidebar class="aside lateral-sidebar" config="$ctrl.config" menu="$ctrl.menu" menu-file="$ctrl.menuFile" clientId="$ctrl.clientId"></cp-sidebar>

                <div class="content-layer m-b-1">
                    <ng-transclude></ng-transclude>
                </div>
            </div>
            `
        });

    })();

Use kebab-case in the template: 在模板中使用kebab-case

angular.module('app').component('application', {
    controller: applicationController,
    template: `
    <cp-layout 
        config="$ctrl.config" 
        menu="$ctrl.menu" 
        ̶c̶l̶i̶e̶n̶t̶I̶d̶=̶"̶$̶c̶t̶r̶l̶.̶c̶l̶i̶e̶n̶t̶I̶d̶"̶
        client-id="$ctrl.clientId"
        logout="$ctrl.logout()">
        <div ui-view class="content-wrapper ng-scope ng-fadeInRight"></div>
    </cp-layout>
    `
});

And use one-way < binding in the component: 并在组件中使用单向<绑定

app.component('cpLayout', {
    bindings : {
        config: '<',
        logout: '&?',
        menu: '<?',
        menuFile: '<?',
        ̶c̶l̶i̶e̶n̶t̶I̶d̶:̶ ̶'̶@̶'̶
        clientId: '<'
    },

For more information, see AngularJS Developer Guide - Component-based application architecture . 有关更多信息,请参阅《 AngularJS开发人员指南-基于组件的应用程序体系结构》

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

相关问题 AngularJs:如何将部分数据从一个组件传递到另一个组件 - AngularJs: How to pass part of my data from one component to another 如何在ng-forward中将函数回调从一个组件传递到另一组件 - How to pass function callback from one component to another component in ng-forward 如何在component.ts中将数据从一个函数传递到另一个函数,angular2 - how to pass data from one function to another in component.ts , angular2 Angular JS将参数从一个控制器传递到另一个 - Angular JS pass parameter one controller to another 将值从资源传递到另一个资源作为参数 - Pass a value from a Resource to another resource as a Parameter 如何将隔离作用域操作与从一个指令到另一个指令的参数一起使用 - How to use isolated scope action with parameter from one directive to another 将值作为参数从ngDialog传递到另一个控制器 - Pass Value as parameter from ngDialog to another controller AngularJs,如何将值从一个页面传递到另一个页面? - AngularJs, how to pass a value from one page to another page? 如何在同一控制器中将数组从一个视图传递到另一个视图 - How to pass an array from one view to another within same controller 如何将控制器中存储的数据从一个页面传递到另一个页面 - How to pass data stored in controller from one page to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM