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

ngMessagesInclude processed after parent ngMessages is removed from DOM #12695

Closed
awerlang opened this issue Aug 27, 2015 · 3 comments
Closed

Comments

@awerlang
Copy link

As the title says, ngMessagesInclude processed after parent ngMessages is removed from DOM. It fails because angular can't find parent controller for linking inner messages. It doesn't happen when ngMessage's are inlined inside the ngMessages element, or while parent element is still present on DOM.

Have an ng-messages like this:

<div ng-if="myForm.$submitted" ng-messages="myForm.myName.$error">
  <div ng-messages-include="messages.html"></div>
</div>

Submission is triggered by:

this.change = function(myForm) {
  var self = this;
  myForm.$setSubmitted();    // event A
  $scope.$applyAsync(function () {
    self.currentStep = 'step2.html';     // event B
  });
};

At event (A), messages.html is downloaded. Because it uses $templateRequest, linking is stalled until server returns. Meanwhile, event (B) proceeds with the removal of ngMessages and its children from DOM. Later, ngMessagesInclude content arrives and angular will compile it. But at that point there's no way to retrieve ngMessages controller anymore.

Plnkr: http://plnkr.co/edit/0GOOLERvfj7n9vDVEyZp?p=preview

Most of the time, it will happen together with an ngIf condition.

Tested against version 1.4.4.

I've found a way to avoid that:

ng-if="myForm.$submitted && myForm.myName.$invalid" 

To solve that at component side, the require: '^^ngMessages' could be made optional and check that condition during linking.

@jpekkala
Copy link
Contributor

An easy fix would be to check if the scope has been destroyed before compiling/linking the HTML in ngMessagesInclude:

$templateRequest(src).then(function(html) {
    if (scope.$$destroyed) return;
    // ...
}

That's how ngInclude handles the same scenario.

@gkalpak
Copy link
Member

gkalpak commented May 13, 2016

Handling this is a similar way as ngInclude sounds reasonable.
@awerlang or @jpekkala, would you be interested in submitting a PR ?

@jpekkala
Copy link
Contributor

Sure. I will submit a PR within a couple of days.

petebacondarwin pushed a commit that referenced this issue May 23, 2016
Messages imported with `ngMessagesInclude` are loaded asynchronously and
they can arrive after the ngMessages element has already been removed from DOM.

Previously we tried to compile these messages and that caused a `$compile:ctreq`
error. Now they are silently ignored if `ngMessagesInclude`'s scope has already
been destroyed.

Closes #12695
Closes #14640
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants