如何将参数从一个组件传递到另一个组件
How to pass parameter from one component to another
我有一些组件,它们一起构成了我的菜单。我想在最上面的组件中获取一个参数,并在下面的组件中使用它。
我想将 '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';
}
这是 cp 布局:
(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>
`
});
})();
在模板中使用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>
`
});
并在组件中使用one-way <
binding:
app.component('cpLayout', {
bindings : {
config: '<',
logout: '&?',
menu: '<?',
menuFile: '<?',
̶c̶l̶i̶e̶n̶t̶I̶d̶:̶ ̶'̶@̶'̶
clientId: '<'
},
有关详细信息,请参阅 AngularJS Developer Guide - Component-based application architecture。
我有一些组件,它们一起构成了我的菜单。我想在最上面的组件中获取一个参数,并在下面的组件中使用它。
我想将 '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';
}
这是 cp 布局:
(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>
`
});
})();
在模板中使用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>
`
});
并在组件中使用one-way <
binding:
app.component('cpLayout', {
bindings : {
config: '<',
logout: '&?',
menu: '<?',
menuFile: '<?',
̶c̶l̶i̶e̶n̶t̶I̶d̶:̶ ̶'̶@̶'̶
clientId: '<'
},
有关详细信息,请参阅 AngularJS Developer Guide - Component-based application architecture。