0

Here's the scenario... I'm using AngularJS and I need to do some modifications to the Datetimepicker extension without modifying the core files or use other extensions.

Anyway, all I needed to do was to clear the timepicker by means of a function (maybe?). The original Timepicker this extension is based upon seems to allow clearance of the time.

Here's my first attempt: plunkr

Here's the function:

  $scope.clearPicker = function() {
    $scope.date = null;
    $scope.date.setHours(0); // Trying to clear the Timepicker
  };

Another attempt is to resort to jQuery: plunkr2

Here's the function:

  $scope.clearPicker = function() {
    $scope.date = null;

    angular.element(document.getElementsByClassName('datetimepicker-wrapper'))
    .find('input')
    .val('');
  };

But of course many would say that the latter is not the Angular way.

So how should I go about injecting functions into an extension?

Giraldi
  • 16,451
  • 6
  • 33
  • 52
  • Possible duplicate of [What are "decorators" and how are they used?](http://stackoverflow.com/questions/16075982/what-are-decorators-and-how-are-they-used) – Martijn Welker Feb 02 '16 at 08:31
  • looking at the code of that directive, the authors *intentionally* designed it to never allow the time to be reset once set, and even have commented in the code in a few places as to why. If you don't intend to modify this directive, then your only alternative would be to contact the authors for an alternative, or post a bug in their bug tracker. – Claies Feb 02 '16 at 08:40
  • @Claies It's times like this that made me prefer jQuery. So there is no way I can manipulate another's directive at all? – Giraldi Feb 02 '16 at 09:25
  • @MartijnWelker: I took a look at your suggestion and I honestly don't know where to start. I'm new to Angular. Any hints? – Giraldi Feb 02 '16 at 09:25
  • of course you can manipulate the directive, but you specifically stated that you didn't want to modify it. – Claies Feb 02 '16 at 09:26
  • @Giraldi A decorator is used to modify 3rd party services/factories/directives without having to change the code in the source, you can read more about it here: http://angular-tips.com/blog/2013/09/experiment-decorating-directives/ (It's an example about directives but it works the save for services) – Martijn Welker Feb 02 '16 at 09:32
  • @Claies: That's exactly what I meant. If given a situation where modifying the core files is not an option, what solution does Angular provides? – Giraldi Feb 02 '16 at 09:42
  • as @MartijnWelker has mentioned, you can use decorators to modify the behavior of 3rd party directives. However, you seem to be insinuating that the Angular Developers are somehow at fault for the 3rd party directive not working the way you expected it to? Otherwise, why would it be Angular's responsibility to provide a solution? – Claies Feb 02 '16 at 09:45
  • Really I'm not trying to blame Angular Devs in any way. I'm just trying to find out how to do this the **Angular way**. I'm just so used to the ease of use of jQuery. I'm still trying to study this **"decorators"** and trying to figure out how to implement it for my situation. – Giraldi Feb 02 '16 at 09:50

0 Answers0