【发布时间】: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}}",但没有任何改变。
如果我使用checked 或checked="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-switchangular指令的来源。
标签: html angularjs checkbox bootstrap-switch