如何将变量传递给Angular UI Bootstrap Datepicker自定义类

时间:2016-08-17 18:30:24

标签: angularjs angular-ui-bootstrap bootstrap-datepicker

我正在尝试将双向数据绑定变量传递给Angular-UI-Bootstrap Datepicker customClass。但是数据绑定在我的customClass函数中不起作用,它只是给了我初始化 chosenUser 变量的值。

示例:

  
      
  1. 我加载网站并使用 chosenUser 中初始化的数据调用 getDayClass (在本例中为undefined)
  2.   
  3. 我从网站上选择了一个用户,双向数据绑定在其他地方正常工作,但在 getDayClass 功能中
  4.   
  5. 所以在 getDayClass selectedUser 仍然显示为未定义,即使它不是
  6.   

因此,在第一次加载 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);
       ...
   };
}

1 个答案:

答案 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]);
           ...
       };
    }