How to pass variable to Angular UI Bootstrap Datep

2019-07-25 00:48发布

I'm trying to pass a two-way data binded variable to Angular-UI-Bootstrap Datepicker customClass. But the data binding doesn't work inside my customClass function, it just gives me the value I initialized chosenUser variable with.

Example:

  1. I load the site and getDayClass gets called with data initialized in chosenUser (undefined in this case)
  2. I choose an user from the site and the two-way data bind works correctly everywhere else but in getDayClass function
  3. So in getDayClass chosenUser is still displayed as undefined, even though it isn't

So any changes made to variables after the first load of getDayClass don't get passed inside that function. I just can't understand why it doesn't work in there.

Here is a stripped version of my angular controller code:

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条回答
再贱就再见
2楼-- · 2019-07-25 00:57

You could try to store 'chosenUser' into an array instead of using plain javascript object notation. Something like:

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]);
           ...
       };
    }
查看更多
登录 后发表回答