0

I need something like this, but it doesn't work

.directive('dateTime', function(){
        return {
            restrict: 'E',
            replace: true,
            scope:'=value',
            template: "<div>{{value.format('mmm dd yy')}}"</div>", 
                                    // ^here: applying a function to the scope  
         };
    });
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
iLemming
  • 34,477
  • 60
  • 195
  • 309

2 Answers2

1

You've created an isolate scope with scope: '=value' so this is a brand new scope that does not prototypically inherit from the parent scope. This means that any functions you want to call must be from

  1. a service, filter, etc. you injected into the directive
  2. another directive's controller, use require to get access (see the tabs and pane directives on the Angular home page for an example)
  3. a function you define in the directive's controller or in the link function on $scope (which are essentially the same thing) Example: https://stackoverflow.com/a/14621193/215945
  4. use the '&' syntax to enable the directive to call a function declared on the parent scope. Example: What is the best way to implement a button that needs to be disabled while submitting in AngularJS?
Community
  • 1
  • 1
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
0

You might just be looking for the date filter:

{{value | date:'MMM dd yy'}}

but you can do this too:

app.directive('dateTime', function(){
        return {
            restrict: 'E',
            replace: true,
            scope:'=value',
            template: "<div>{{value | date:'MMM dd yy')}}"</div>", 
                                    // ^here: applying a function to the scope  
         };
    });
Ben Lesh
  • 107,825
  • 47
  • 247
  • 232
  • well it's not the same... value can be not exactly date time format, I need to apply a function, any arbitrary function to it – iLemming Feb 04 '13 at 20:48
  • so you need to call a scope function from the directive? – Liviu T. Feb 04 '13 at 20:52
  • I have a directive, in its template I use another directive that suppose to call some arbitrary function and apply it to the value – iLemming Feb 04 '13 at 21:05