我试图在角度JS中使用noUiSlider,我有一个包含4个滑块的指令,我希望在2页上显示这个指令。 当我更改页面时出现错误:错误:滑块已经初始化。
如果我刷新页面,我不会遇到此错误,但这不是我想要的:)
我看到一个"解决方案"谁包括直接修改noUiSlider.js的代码,但我不想这样做
var familyLevel = document.getElementById('family-level');
var moneyLevel = document.getElementById('money-level');
var securityLevel = document.getElementById('security-level');
var hobbiesLevel = document.getElementById('hobbies-level');
service.initSliders(scope);
if(Number.isInteger(scope.owner))
{
priorities.getPriorities(scope.owner).then(function(data){
console.log(data);
if(angular.isObject(data)){
angular.forEach(scope.model.priorities, function(value, key){
if(data[key]) scope.model.priorities[key] = data[key];
});
service.initSliderValues(scope);
}
}).catch(function(error){
console.error(error);
});
}
else{
appAuth.currentUser().then(function(user) {
priorities.getPriorities(user.id).then(function(data){
if(angular.isObject(data)){
angular.forEach(scope.model.priorities, function(value, key){
if(data[key]) scope.model.priorities[key] = data[key];
});
service.initSliderValues(scope);
}
});
});
}
},
initSliderValues: function(scope){
familyLevel.noUiSlider.set(scope.model.priorities.family);
familyLevel.noUiSlider.on('set', function (val, hand) {
familyLevel.setAttribute("data-level", val[hand]);
scope.model.priorities.family = val[hand];
});
moneyLevel.noUiSlider.set(scope.model.priorities.money);
moneyLevel.noUiSlider.on('set', function (val, hand) {
moneyLevel.setAttribute("data-level", val[hand]);
scope.model.priorities.money = val[hand];
});
securityLevel.noUiSlider.set(scope.model.priorities.security);
securityLevel.noUiSlider.on('set', function (val, hand) {
securityLevel.setAttribute("data-level", val[hand]);
scope.model.priorities.security = val[hand];
});
hobbiesLevel.noUiSlider.set(scope.model.priorities.hobbies);
hobbiesLevel.noUiSlider.on('set', function (val, hand) {
hobbiesLevel.setAttribute("data-level", val[hand]);
scope.model.priorities.hobbies = val[hand];
});
},
initSliders: function (scope) {
var sliders = $('.slider-range');
for (var i = 0; i < sliders.length; i++) {
try{
noUiSlider.create(sliders[i], {
start: 2,
step: 1,
connect: 'lower',
range: {
'min': 0,
'max': 5
},
format: {
to: function (value) {
return value;
},
from: function (value) {
return value;
}
}
}, true);
}catch(err){
sliders[i].noUiSlider.destroy();
service.initSliders(scope);
}
}
// initSlider=true;
}
我修改了我的代码以在catch中添加destroy,我没有错误但是滑块的值没有设置为我的数据
答案 0 :(得分:0)
我的解决方案,它的工作。
sliders[0].noUiSlider.set(scope.model.priorities.family);
sliders[0].noUiSlider.on('set', function (val, hand) {
sliders[0].setAttribute("data-level", val[hand]);
scope.model.priorities.family = val[hand];
});
sliders[1].noUiSlider.set(scope.model.priorities.money);
sliders[1].noUiSlider.on('set', function (val, hand) {
sliders[1].setAttribute("data-level", val[hand]);
scope.model.priorities.money = val[hand];
});
sliders[2].noUiSlider.set(scope.model.priorities.security);
sliders[2].noUiSlider.on('set', function (val, hand) {
sliders[2].setAttribute("data-level", val[hand]);
scope.model.priorities.security = val[hand];
});
sliders[3].noUiSlider.set(scope.model.priorities.hobbies);
sliders[3].noUiSlider.on('set', function (val, hand) {
sliders[3].setAttribute("data-level", val[hand]);
scope.model.priorities.hobbies = val[hand];
});
如果有人想要优化这部分......;)