We have answer of your question!

100% solved queries, no empty question

Question: AngularJS: How do you return variables or modify the $scope from a directive?


0

Advertisement


I cannot for the life of me get the vars 'start' and 'end' to return to my function in my controller.

l() == console.log()

I'm trying to turn this: http://www.dangrossman.info/2012/08/20/a-date-range-picker-for-twitter-bootstrap/

Into a directive for this: https://github.com/clouddueling/angularjs-common

controller.js

ontrollers.controller('PatientsCtrl', function($scope, $http, $location, $cookies, Popup, Value, Users, User, System){
    $scope.loadUserSystems = function(start, end) {
        l(start);
        l(end);

        return;
        $scope.activeUser = this.user ? this.user : $scope.activeUser;
        User.systems($scope.activeUser.id, start, end).success(function(data){
            $scope.activeSystems = data.systems;
        });
    }


    $scope.dateRangeOptions = {
      ranges: {
                'Today': [new Date(), new Date()],
                'Yesterday': [moment().subtract('days', 1), moment().subtract('days', 1)],
                'Last 7 Days': [moment().subtract('days', 6), new Date()],
                'Last 30 Days': [moment().subtract('days', 29), new Date()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract('month', 1).startOf('month'), moment().subtract('month', 1).endOf('month')]
      },
      format: 'MM/DD/YYYY',
      separator: ' to ',
      startDate: moment().subtract('days', 29),
      endDate: new Date(),
      minDate: '01/01/2012',
      maxDate: '12/31/2013',
      locale: {
          applyLabel: 'Submit',
          fromLabel: 'From',
          toLabel: 'To',
          customRangeLabel: 'Custom Range',
          daysOfWeek: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr','Sa'],
          monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
          firstDay: 1
      },
      showWeekNumbers: true,
      buttonClasses: ['btn-danger'],
      dateLimit: false
   }
});

partial.html

        <div class='btn'>
            <i class='icon-calendar'></i>
            <span
                date-range-picker
                options='dateRangeOptions'
                change='loadUserSystems(start, end)'></span>
        </div>

directive.js

directives.directive('dateRangePicker', function ($parse) {
  return {
    restrict: 'A',
    scope: {
      options: '=',
    },
    template: "<span></span> <b class='caret'></b>",
    link: function (scope, element, attr) {
      $(element).daterangepicker(scope.options, function(start, end){
        $(element).find('span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));

        // Retrieve the callback function
        var fn = $parse(attr.change);

        scope.$apply(function () {
          fn(scope, { start: start, end: end });
        });
      });

      $(element).find('span').html(moment().format('MMMM D, YYYY') + ' - ' + moment().format('MMMM D, YYYY'));
    }
  };
});
Question author Michael-j-calkins | Source

Answer


1


Advertisement


Try this:

directives.directive('dateRangePicker', ['$window', function ($window) {
  return {
    restrict: 'A',
    scope: {
      options: '=',
      onChange: '&change'
    },
    template: "<span></span> <b class='caret'></b>",
    link: function (scope, element, attr) {
      $(element).daterangepicker(scope.options, function(start, end){
        $(element).find('span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));

        scope.$apply(function () {
          scope.onChange({ start: start, end: end });
        });
      });

      $(element).find('span').html($window.moment().format('MMMM D, YYYY') + ' - ' + $window.moment().format('MMMM D, YYYY'));
    }
  };
}]);

Also, the above assumes moment() is on the window. You can find out more about executing a expressions in the context of the parent scope here.

Edit Fixing onChange call.

Answer author Rtcherry

Advertisement


Tickanswer.com is providing the only single recommended solution of the question AngularJS: How do you return variables or modify the $scope from a directive? under the categories i.e angularjs , angularjs-directive , angularjs-scope , . Our team of experts filter the best solution for you.

Related Search Queries:

You may also add your answer!