AngularJs-引导切换无法获取复选框值
我正在为我的AngularJs项目使用。我为这个插件创建了angular指令AngularJs-引导切换无法获取复选框值,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我正在为我的AngularJs项目使用。我为这个插件创建了angular指令 myApp.directive('iosToggle', function(){ return { restrict: 'A', link: function(scope, element, attrs){ $(element).bootstrapToggle(scope.$eval(attrs.iosToggle)); } };
myApp.directive('iosToggle', function(){
return {
restrict: 'A',
link: function(scope, element, attrs){
$(element).bootstrapToggle(scope.$eval(attrs.iosToggle));
}
};
});
并且在我看来使用指令
<input type="checkbox" id="myInterview" data-ios-toggle="toggle" data-style="ios" data-onstyle="info" data-size="small" data-on="On" data-off="Off" ng-model="myData.myInterview">
我得到了相同的设计,可以打开或关闭,但当我提交表单时,我没有得到复选框值。是的,我已使用$watch on change更新了指令,然后更新了模型。它起作用了
myApp.directive('iosToggle', function($timeout){
return {
restrict: 'A',
transclude: true,
replace: false,
require: 'ngModel',
link: function ($scope, $element, $attr, ngModel) {
// update model from Element
var updateModelFromElement = function() {
// If modified
var checked = $element.prop('checked');
if (checked !== ngModel.$viewValue) {
// Update ngModel
ngModel.$setViewValue(checked);
$scope.$apply();
}
};
// Update input from Model
var updateElementFromModel = function() {
$element.trigger('change');
};
// Observe: Element changes affect Model
$element.on('change', function() {
updateModelFromElement();
});
// Observe: ngModel for changes
$scope.$watch(function() {
return ngModel.$viewValue;
}, function() {
updateElementFromModel();
});
// Initialise BootstrapToggle
$timeout(function() {
$element.bootstrapToggle();
});
}
};
});
你也必须观看和更改ng模型!工作正常,来源如下: