ionic-framework: $ionicView.beforeEnter is not fired

after the update from ionic 1.3.0 to 1.3.1 the $ionicView.beforeEnter is not fired any more as before

I have this controller:

class MyCtrl {
 static $inject = [   
    "$scope"
  ];
  constructor(
    private $scope
  ) { 
    $scope.$on("$ionicView.beforeEnter", (event, data) => {
      this.activate();
    });
  }
}

in version 1.3.0 the activate() method is called as expected in version 1.3.1 the activate() method is not called

the fix is to roll back to version 1.3.0

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 23 (8 by maintainers)

Most upvoted comments

This should probably be listed in the change log under “breaking changes”. +1

Before ($ionicView.beforeEnter is not fired) :

<ion-header-bar>
...
</ion-header-bar>
<ion-view>
...
</ion-view>

After ($ionicView.beforeEnter is fired):

<div>
<ion-header-bar>
...
</ion-header-bar>
<ion-view>
...
</ion-view>
</div>

@danbucholtz is right, ensuring there is only 1 root element in your template fixes this issue.

I had this issue occur when I had the following:

<ion view>
  stuff here
</ion-view>

<script id="some-template.html" type="text/ng-template">
  some template
</script>

Moving the <script> block to inside the <ion-view> fixed it for me.

Hi @asmund1,

When I use your Plunkr, I do see the issue. However, it is because your template is not a container. That’s an edge case so I’m not going to make a change right now to support that. If you change your template to the following, it works correctly.

<ion-view>
  <h1>View one</h1>
</ion-view>

@rxnh8255, @darthdie, Please help me out with what is unique with your apps. Can you make a plunkr or a codepen demonstrating the issue? I cannot recreate it presently. Please let me know. Thanks for the details @darthdie. Why is your stateValue null?

Please let me know and I’ll get it resolved.

Thanks, Dan

In my testing it appears this was broken in commit d63733350b73c0084f99fe285dda96f0204b2cd7

Specifically the following line returns null (thus it then has no scope to emit events to)

var enteringScope = getScopeForElement(enteringEle, enteringData);

From further testing it appears it returns null because inside of getScopeForElement

    if ( attributeValue !== "true" ) {
      // it's not an abstract view, so make sure the element
      // matches the state.  Due to abstract view weirdness,
      // sometimes it doesn't. If it doesn't, don't dispatch events
      // so leave the scope undefined
      if ( stateValue === stateData.stateName ) {
        return angular.element(element).scope();
      }
      return null;
    }

The condition if ( stateValue === stateData.stateName ) is false because stateValue is null. (At least in my particular case)

In case anybody is interested (and/or needs these events to fire like myself) for now I’ve patched it simply by merging the original with the new (inside of the emit function in “$ionicViewSwitcher”), i.e.

var enteringScope = getScopeForElement(enteringEle, enteringData) || enteringEle.scope();