Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

directives with isolated scope and a optional function '&?' should be returned as undefined if the function is missing #6404

Closed

Description

@davidemoro

Overview of the issue

It is not a bug, probably just a design issue about directives with isolated scope and optional functions passed via '&?'.

If you don't pass any function to the directive, you get a function that returns undefined instead of undefined.

Errors

No errors are thrown.

Motivation for or Use Case

If you declare a scope property as optional, probably the expected behaviour is getting undefined if you try to access this property.
This is exactly what happens when you have a '@?' binding and you don't fill in the optional attribute: you get undefined trying to access $scope.property.

The way to understand if there is a function parameter is inspecting the attrs.
So this seems a special case not motivated for '&?'.

Angular Version(s)

All versions, no regression.

Browsers and Operating System

All browsers and operting systems.

Suggestion

In order to get a common behaviour for optional parameters you could just add the following control to

case '&':
:
if (optional && !attrs[attrName]) {
return;
}

If you find that this proposal is ok I can make a pull request from: https://github.com/davidemoro/angular.js/blob/master/src/ng/compile.js

I've just run the tests and all seems fine.

Activity

  1. self-assigned this
    on Feb 22, 2014
  2. added this to the Ice Box milestone on Feb 22, 2014
  3. btford commented on Feb 22, 2014

    @btford
    Contributor

    Thanks for this! I think this will take some serious consideration since changes to $compile tend to have far-reaching effects and unintended consequences.

    I don't have a particularly strong opinion on this.

    @IgorMinar @caitp @tbosch what do you think?

  4. removed their assignment
    on Feb 22, 2014
  5. caitp commented on Feb 22, 2014

    @caitp
    Contributor

    Right now, the compiler doesn't really track whether or not an expression makes a function call or not (or how many, as it could be more than one). I don't feel like this really makes a lot of sense.

    If we did force an expression to do something meaningful (for some definition of "meaningful", by throwing if it doesn't), this would break some apps.

    I guess it's a tradeoff between notifying people that they might be doing something they don't expect to do, but I think that given how hard it is to tell if an expression is doing something useful, it's probably not a good idea to change this.

    edit: It isn't that bad to make the "optional" flag actually do something for & properties, though. That seems okay with me.

    edit again: actually, making these optional would still be a breaking change for directives using optional-flagged expression properties, because it would require the directive to test for the presence of the scope property before evaluating the expression. This seems bad.

  6. IgorMinar commented on Feb 22, 2014

    @IgorMinar
    Contributor

    I agree that the current behavior is weird. It is not intentional.

    We should fix this in 1.3

  7. modified the milestones: , Ice Box on Feb 22, 2014
  8. davidemoro commented on Feb 22, 2014

    @davidemoro
    Author

    Ok, how can I help you? I want to dive into angular and there are tickets that needs a PR, so I would be glad to contribute.
    I just need a mentor that reviews my code

  9. 9 remaining items

  10. self-assigned this
    on Sep 9, 2014
  11. added 5 commits that reference this issue on Sep 22, 2014
    4e6a1fd
    e43361b
    28907bf
    6922ffb
    1df4e70
  12. yellow1912 commented on Dec 16, 2014

    @yellow1912

    I think there should be a way to check if the function is passed in or not, right now it's just too weird. I have to manually check if the attribute exists.

  13. added 2 commits that reference this issue on Jan 30, 2015
    bea0482
    4f55ebf
  14. sramilli commented on Sep 7, 2016

    @sramilli

    Hi,
    i still see this behaviour in Angular 1.5.8
    i´m passing an optional function to a sub-sub-component with "&?" in both steps.
    The first sub-component detects "undefined", but the sub-sub-component got a function instead.

    In my sub-component's template i would like to avoid having some logic toggling between
    <sub-sub-component some-attribute="$ctrl.anAttribute">.......</...>
    and
    <sub-sub-component some-attribute="$ctrl.anAttribute" optional-function="$ctrl.myFunction()">.....</...>

    As a workaround i use instead "=" as shown
    bindings: { myFunction: "=" }

  15. ashclarke commented on Sep 7, 2016

    @ashclarke

    @sramilli I had the exact same issue today (1.5.8 too). Workaround I applied was the same too.

    (Fun fact: I came to this thread again via Google, and then got freaked out just now because I got this notifcation. Wasn't until I visited GitHub to reply that I realised that I'd already participated in this issue over 2 years ago.)

  16. tabanliviu commented on Feb 27, 2017

    @tabanliviu

    @davidemoro @caitp I'd like to mention that we recently hit upon this issue during our upgrade from 1.3 to 1.5 and our unit tests did not capture this behavior because in 1.3 there was no difference in the outcome between using a directive without the optional attribute vs having an optional value that is undefined.

    I think this should have been marked as a breaking change or at least had some note about potential breaking change.

    I admit I didn't read the full change log line by line and only looked at the breaking changes section but still looking back I would have preferred some hint about this change.

  17. tommck commented on Sep 17, 2018

    @tommck

    This problem still exists in 1.6.10

    Optional expression is always a function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions