我在 Angular 1.5 项目中也遇到了同样的问题,我还使用了 angular-ui-router 和 Angular Material 组件。一种可能的解决方案是简单地使用string-based binding,将两个属性绑定到组件中:layout 和 flex。当您通过以下方式指定您的 ui 路由状态时,您可以为这两个绑定属性(通常分别为 'row' 和 '100' )提供值:
var exampleState = {
name: 'examplestate',
url: '/', // change this to your desired path
component: 'example',
resolve: {
layout: function () {
return 'row'; // you can change this value depending on your UI requirements
},
flex: function () {
return '100'; // you can change this value depending on your UI requirements
}
}
};
在模块的 config 方法中,您以标准方式定义您的 exampleState:
$stateProvider.state(exampleState);
最后,在组件定义中,当您定义绑定时,将这两个属性添加到绑定属性列表中。
打字稿中的示例:
export class ExampleComponent implements ng.IComponentOptions {
public bindings: any;
public controller: any;
public template: string;
constructor() {
this.bindings = {
layout: '@',
flex: '@'
};
this.controller = ExampleComponentController;
this.template = ExampleViewTemplate;
}
}
您可以按照通常的方式将此组件添加到您的 Angular 模块中:
.component('example', new ExampleComponent());
这将在 DOM 中生成以下 HTML 元素(由 angular-ui-router 自动创建):
<example layout="row" flex="100" class="ng-scope ng-isolate-scope layout-row flex-100">
因此,通过这种方式,您无需在代码中插入任何手动 CSS 样式。您正在组件元素上使用标准的 Angular Material 属性。