[英]Is custom input module a good practice in Angular?
App I'm building, all inputs look exactly the same (Design wise). 我正在构建的应用程序,所有输入看起来完全一样(在设计方面)。
I am feeling I am repeating my self every time with the following lines: 我感觉我每次都会重复以下几行:
HTML HTML
<div class="myInputContainer">
<input
type="text"
class="myInputView-input"
aria-label="login_email"
placeholder="Filter Menu Items..."/>
</div>
CSS CSS
.myInputContainer {
@extend %input-like-container;
}
.myInputView-input {
@extend %input-like;
width: 100%;
}
I am considering creating an Input module and using it instead of repeating code 我正在考虑创建一个Input模块并使用它而不是重复代码
<my-input placeholder="foo">
My question is: 我的问题是:
Is this good practice to create a module of a base UI component? 创建基本UI组件的模块是一种好的做法吗? (Using module is the only way all other modules can import and use it) (使用模块是所有其他模块可以导入和使用的唯一方法)
All examples applications I see use only default <input>
and I am not really sure why. 我看到的所有示例应用程序仅使用默认的<input>
,我不确定为什么。
Thanks. 谢谢。
Speaking about angular - yes. 说到角度-是的。 Angular is a component-based framework. Angular是基于组件的框架。 So here how we do in our project. 这就是我们在项目中的工作方式。 We have a base module for common components. 我们有一个用于通用组件的基本模块。
myapp.common
I have a bunch of custom components in it. 我有一堆自定义组件。
myapp.common.input
myapp.common.select
Check out what AngularJS team suggest: 检查出什么 AngularJS团队建议:
While the example above is simple, it will not scale to large applications. 尽管上面的示例很简单,但无法扩展到大型应用程序。 Instead we recommend that you break your application to multiple modules like this: 相反,我们建议您将应用程序分成多个模块,如下所示:
- A module for each feature 每个功能的模块
- A module for each reusable component (especially directives and filters) 每个可重用组件的模块(尤其是指令和过滤器)
- And an application level module which depends on the above modules and contains any initialization code. 还有一个应用程序级别的模块,它依赖于上述模块,并且包含任何初始化代码。
Here is how my input component looks like: 这是我的输入组件的外观:
(function () {
/* @ngInject */
function InputComponent() {
const ctrl = this;
ctrl.$onInit = function () {
ctrl.ngModel.$render = function () {
ctrl.innerModel = ctrl.ngModel.$viewValue;
};
ctrl.onChange = function () {
ctrl.ngModel.$setViewValue(ctrl.innerModel);
};
};
}
angular
.module('myapp.common.input')
.component('myappInput', {
controller: InputComponent,
require: {
ngModel: 'ngModel',
},
bindings: {
placeholder: '@',
type: '@',
title: '@',
},
template:
`<div class="form-group">
<label class="myapp-input-title">
{{ ::$ctrl.title }}
</label>
<input
ng-model="$ctrl.innerModel"
ng-change="$ctrl.onChange()"
placeholder="{{ ::$ctrl.placeholder }}"
type="{{ ::$ctrl.type }}"
</div>`,
});
}());
The little snippet in $onInit
is required to bind custom component ngModel to input model. $onInit
的小片段是将自定义组件ngModel绑定到输入模型所必需的。
So now I simply use this 所以现在我只用这个
<myapp-input ng-model='modelName'></myapp-input>
Here are some benefits: 这里有一些好处:
myapp.custom.input
to the test suite and make sure that you testing this exact peace of code 易于编写的单元测试-您只需将myapp.custom.input
包含到测试套件中,并确保您测试了这种完全和平的代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.