我正在尝试将双向数据绑定变量传递给Angular-UI-Bootstrap Datepicker customClass。但是数据绑定在我的customClass函数中不起作用,它只是给了我初始化 chosenUser 变量的值。
示例:
- 我加载网站并使用 chosenUser 中初始化的数据调用 getDayClass (在本例中为undefined)
- 我从网站上选择了一个用户,双向数据绑定在其他地方正常工作,但在 getDayClass 功能中
- 所以在 getDayClass selectedUser 仍然显示为未定义,即使它不是
醇>
因此,在第一次加载 getDayClass 之后对变量所做的任何更改都不会在该函数内部传递。我只是无法理解为什么它在那里不起作用。
以下是我的角度控制器代码的剥离版本:
function CalendarController() {
var vm = this;
//THIS IS THE VARIABLE (USER OBJECT) I'M TRYING TO PASS TO getDayClass
vm.chosenUser = {};
//OPTIONS PASSED TO BOOTSTRAP DATEPICKER
vm.options = {
customClass: getDayClass
};
//THIS IS THE FUNCTION I RUN FROM HTML WITH NG-CLICK AND IT EXECUTES
//FUNCTION getDayClass (AMONG OTHER THINGS)
vm.refresh = function() {
$("#calendar").data("$uibDatepickerController").refreshView();
}
function getDayClass(data) {
var date = data.date,
mode = data.mode;
//RETURNS UNDEFINED (ONLY INSIDE THIS FUNCTION)
//EVEN AFTER USER HAS BEEN CHOSEN
console.log(vm.chosenUser);
...
};
}
答案 0 :(得分:1)
您可以尝试存储“已选择用户”。到数组而不是使用普通的javascript对象表示法。类似的东西:
function CalendarController() {
var vm = this;
//THIS IS THE VARIABLE I'M TRYING TO PASS TO getDayClass
vm.chosenUser = ["UserNameWillBeHere"];
//OPTIONS PASSED TO BOOTSTRAP DATEPICKER
vm.options = {
customClass: getDayClass
};
//THIS IS THE FUNCTION I RUN FROM HTML WITH NG-CLICK AND IT EXECUTES
//FUNCTION getDayClass (AMONG OTHER THINGS)
vm.refresh = function() {
$("#calendar").data("$uibDatepickerController").refreshView();
}
function getDayClass(data) {
var date = data.date,
mode = data.mode;
//RETURNS UNDEFINED (ONLY INSIDE THIS FUNCTION)
//EVEN AFTER USER HAS BEEN CHOSEN
console.log(vm.chosenUser[0]);
...
};
}