【问题标题】:Bootstrap switchand angular initialization引导开关和角度初始化
【发布时间】:2017-10-02 21:55:43
【问题描述】:

bootstrap switch 初始化有问题

<span style="margin-right: 5px;">{{accessory.name}} </span> 
   <input   type="checkbox" name="accessory{{accessory.id}}"
       data-ng-model="accessory.value" data-ng-attr-id="{{accessory.id}}"
       data-ng-attr-topic="{{accessory.topic}}" bootstrap-switch>

ng-model 不初始化状态,它总是假的。我什至尝试过ng-checked="{{accessory.value}}",但没有任何改变。 如果我使用checkedchecked="checked" 一切正常。 你有什么建议吗?

这是引导开关的指令:

app.directive('bootstrapSwitch', 
        ['$http',
         function($http) {
            return {
                restrict: 'A',
                require: '?ngModel',
                link: function(scope, element, attrs, ngModel) {
                    element.bootstrapSwitch({size: "small", onColor:"success", offColor:"danger"});
                    //ngModel.$setViewValue(false); //set start status to false
                    element.on('switchChange.bootstrapSwitch', function(event, state) {
                        if (ngModel) {
                            scope.$apply(function() {
                                ngModel.$setViewValue(state);
                            });
                        }
                    });

                    scope.$watch(attrs.ngModel, function(newValue, oldValue) {
                        if (newValue!= oldValue){
                            $http({
                                method: 'PUT',
                                url: '/reservations/' + document.getElementById("hiddenIdReservation").value + '/accessories/'+ element[0].attributes.id.value,
                                params: {topic: element[0].attributes.topic.value, value: newValue}
                            }).then(function successCallback(response) {
                                if (typeof response.data.success == 'undefined'){
                                    window.location.href = "/500";
                                }else if (response.data.success==true){
                                    if (newValue) {
                                        element.bootstrapSwitch('state', true, true);
                                    } else {
                                        element.bootstrapSwitch('state', false, true);
                                    }
                                }else if (response.data.success==false)
                                    notifyMessage(response.data.result, 'error');
                            }, function errorCallback(response) {
                                window.location.href = "/500";
                            });
                        }
                    });
                }
            };
        }
        ]
);

ngModel.$setViewValue(false); 的注释为真,但开关仍处于关闭状态。

【问题讨论】:

  • 您使用什么引导开关库来执行此操作?
  • bootstrapswitch.com 在主帖中指定
  • 这是一个jquery插件,我知道,我想要你正在使用的bootstrap-switch angular指令的来源。

标签: html angularjs checkbox bootstrap-switch


【解决方案1】:

初始化时,JQuery 初始化函数错过了 ng-checked 指令。所以我的建议是将ng-model 值更新为switch

var app = angular.module('myApp', []);
app.controller('MyController', function MyController($scope) {
  $scope.accessory = {
    name: "test",
    id: 1,
    value: "true",
    topic: 'test'
  };
  $scope.testing = true;
}).directive('bootstrapSwitch', ['$http',
  function($http) {
    return {
      restrict: 'A',
      require: 'ngModel',
      link: function(scope, element, attrs, ngModel) {
        element.bootstrapSwitch({
          size: "small",
          onColor: "success",
          offColor: "danger"
        });
        element.on('switchChange.bootstrapSwitch', function(event, state) {
          if (ngModel) {
            scope.$apply(function() {
              ngModel.$setViewValue(state);
            });
          }
        });
        scope.$watch(ngModel, function(newValue, oldValue) {
          if (newValue) {
            element.bootstrapSwitch('state', true, true);
          } else {
            element.bootstrapSwitch('state', false, true);
          }
        });
      }
    }
  }
]);

这是一个相同的工作示例。

JSFiddle Demo

我忽略了 scope.$watch 的东西,因为它不会导致问题!

【讨论】:

  • 我在 ng-repeat 中有这个,这是一个有效的解决方案吗?
  • @luca 测试一下并告诉我:D
  • 即使不重复复选框现在总是正确的,在 element.bootstrapSwitch('state', scope.bootstrapSwitch);它变成了真实的变量也是错误的......现在我正在调试
  • @luca 你想使用ng-modelchecked 属性来更新切换吗?
  • 最好ng-model。我用element.bootstrapSwitch('state', scope.bootstrapSwitch == 'true'); 修复了上面的代码
猜你喜欢
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-05
  • 1970-01-01
  • 2022-01-01
  • 2020-08-23
  • 2017-06-28
相关资源
最近更新 更多