【发布时间】:2020-01-29 20:28:14
【问题描述】:
我们在项目中使用 AngularJS 1.6 和 bootstrap 4.3.1 来构建 UI。现在我想构建一个简单的组件,它基本上可以更轻松地使用引导程序的表单组元素。
我尝试这样编写组件:
模板:
<div class="form-group">
<label
for="input-{{::$ctrl.name}}"
class="input-label mb-1"
ng-class="{'disabled': $ctrl.isDisabled, 'required': $ctrl.isRequired}"
ng-bind="::$ctrl.label">
</label>
<input
tabindex="{{$ctrl.tabindex}}"
ng-readonly="$ctrl.isReadonly"
ng-model="$ctrl.model"
type="{{::$ctrl.type}}"
id="input-{{::$ctrl.name}}"
class="form-control"
ng-class="{'negative': $ctrl.hasNegativeStyle}"
ng-required="$ctrl.isRequired">
JavaScript:
(function () {
'use strict';
var component = {
templateUrl: 'app/components/bootstrap/form-group-input/form-group-input.component.html',
bindings: {
name: '@',
label: '@',
model: '<',
isReadonly: '<',
isRequired: '<',
isDisabled: '<',
hasNegativeStyle: '<',
type: '@',
tabindex: '@'
}
};
angular
.module('collphir.common')
.component('cwpFormGroupInput', component);
})();
现在的问题是模型绑定。更改组件中的输入不会影响父模型,因为它是单向绑定的。但是,如果不回到旧的双向绑定(我们不想要,因为我们想很快迁移到 Angular),我怎么能实现呢?
【问题讨论】:
标签: angularjs components