一个bootstrap表单组中的2指令不起作用

时间:2015-11-03 08:37:56

标签: angularjs twitter-bootstrap angularjs-directive directive

我有一个bootstrap form-group,其中有2个指令

<div class="form-group">
                        <label for="Name" class="col-md-2">Name</label>
                        <sw-textbox id="Name" sw-class="col-md-3" sw-model="Entity.Name" />


                        <label for="Family" class="col-md-2">Family</label>
                        <sw-textbox id ="Family" sw-class="col-md-3" sw-model="Entity.Family" />

                    </div>

我的指令就像

app.directive('swTextbox', function () {
    return {
        restrict: 'E,A',
        scope:
            {
                swModel: '=',
                swWidth: '=',
                swHeight: '=',
                swNullable: '=',
                swReadonly: '=',
                swDisable: '=',
                swVisible: '=',
                swMultiline: '=',
                swClass: '@',
                swChangedValue: '&changedvalue'
            },
        template: '<input type="text" dir="rtl" ng-model="swModel" ng-model-options="{updateOn: \'blur\'}" ng-change="swChangedValue({newVal: swModel})" />',
        link: function (scope, el, attr) {
            if (scope.swMultiline) {
                var htm = '<textarea dir="rtl"  ng-model="swModel" ng-model-options="{updateOn: \'blur\'}" ng-change="swChangedValue({newVal: swModel})"';
                if (angular.isDefined(attr.id))
                    htm += 'id="' + attr['id'] + '"';
                if (scope.swDisable)
                    htm += ' disabled';
                if (scope.swReadonly)
                    htm += ' readonly';
                if (scope.swHeight != undefined || scope.swWidth != undefined || scope.swNullable != undefined) {
                    htm += ' style="';
                    if (scope.swHeight != undefined)
                        htm += 'max-height:' + scope.swHeight + 'px; height:' + scope.swHeight + 'px;';
                    if (scope.swWidth != undefined)
                        htm += 'max-width:' + scope.swWidth + 'px; width:' + scope.swWidth + 'px;';
                    if (scope.swReadonly == undefined && scope.swDisable == undefined && scope.swNullable != undefined)
                        htm += 'background-color:#fcf3f8;';
                    htm += '"';
                }
                htm += '></textarea>';
                el.find('input').replaceWith(htm);
            }
            else {
                scope.$watch('swWidth', function (newValue, oldValue, scope) {
                    el.find('input').css('width', newValue);
                });
                scope.$watch('swNullable', function (newValue, oldValue, scope) {
                    el.find('input').css('background-color', (!newValue) ? '#fcf3f8' : '');
                });
                scope.$watch('swReadonly', function (newValue, oldValue, scope) {
                    el.find('input').attr('disabled', newValue);
                    el.find('input').css('background-color', (newValue) ? '#f3f3f3' : (scope.swNullable == undefined || scope.swNullable) ? '' : '#fcf3f8');
                });
                scope.$watch('swDisable', function (newValue, oldValue, scope) {
                    el.find('input').attr('disabled', newValue);
                    el.find('input').css('background-color', (newValue) ? '#f3f3f3' : (scope.swNullable == undefined || scope.swNullable) ? '' : '#fcf3f8');
                });
                scope.$watch('swVisible', function (newValue, oldValue, scope) {
                    if (newValue != undefined)
                        el.find('input').css('display', (newValue) ? '' : 'none');
                });
                scope.$watch('swClass', function (newValue, oldValue, scope) {
                    if (newValue != undefined)
                        el.find('input').addClass(newValue);
                });
            }
        }
    };
});

当我在表单中单独使用sw文本框时,没有任何问题,但每当我像这样使用它时 第二个sw文本框未呈现

可能是什么原因?

1 个答案:

答案 0 :(得分:1)

使用尾随固相可能是一个问题。尝试使用结束标记。

<div class="form-group">
    <label for="Name" class="col-md-2">Name</label>
    <sw-textbox id="Name" sw-class="col-md-3" sw-model="Entity.Name"></sw-textbox>
    <label for="Family" class="col-md-2">Family</label>
    <sw-textbox id ="Family" sw-class="col-md-3" sw-model="Entity.Family"></sw-textbox>
</div>

查看这个快速的弹药http://plnkr.co/edit/ZX4sgkoaKbBZxOGKsLTQ