Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS ng-include on ng-click

I would like to find a way to insert HTML (which is optimalized for the controller) into alert div. However I couldn't find a way to do it...

<script type="text/ng-include" id="login.html">
       <form data-select="exeption" class="loginBox shadowed" onclick="event.stopPropagation();" novalidate name="login">
        <h2>Login alert</h2>
            <!--inputs and such, they need to be controlled by the controller-->
    </form>
</script>
<script type="text/ng-include" id="bug.html">
    <form data-select="exeption" class="bugBox shadowed" onclick="event.stopPropagation();" novalidate name="report">
        <h2>Bug report</h2>
            <!--inputs and such, they need to be controlled by the controller-->
    </form>
</script>

This two templates should be evoked by the JS itself or by user. Those templates should get into this div, but I can't use innerHTML since in templates are some ng-models and such things...

<div id="alert" data-ng-click="empty()" data-ng-controller="alert" role="navigation"></div>
like image 762
Akxe Avatar asked Dec 26 '22 11:12

Akxe


1 Answers

Usually what I do is use ng-if / ng-show . I'm not sure I understood your request correctly, so I'll write a little example; let's say you have a simple login form:

<form>
  <label>
    username:
    <input name="username" type="text" ng-model="username"/>
  </label>
  <label>
    password:
    <input name="password" type="password" ng-model="password"/>
  </label>
  <button type="submit" ng-click="login()">login</button>
  <div class="message" ng-if="message">
  </div>
</form>

Inside the controller:

$scope.username = '';
$scope.password = '';

$scope.message = '';

$scope.login = function() {
  // login example function with ajax request and success/error promises
  myLogin($scope.username, $scope.password)
    .success(function() {
      $scope.message = 'Logged in!';
    })
    .error(function(errorMessage) {
      $scope.message = errorMessage;
    })
}

This way your div is empty when $scope.message is empty and you can show it automatically just giving $scope.message a value. If you need to have an ng-include, simplest thing you could do is to use it inside a div that you show when you need it:

<div ng-if="showMessage">
  <div ng-include="template.html"/>
</div>

UPDATE: following my last example, if you wanted to include a different type of message for every situation, you could use multiple ngIf, including different template; example:

<div ng-if="showMessage">
  <div ng-if="message.type == 'alert'" ng-include="'alert.html'"/>
  <div ng-if="message.type == 'info'" ng-include="'info.html'"/>
  <div ng-if="message.type == 'warning'" ng-include="'warning.html'"/>
  <div ng-if="message.type == 'error'" ng-include="'error.html'"/>
</div>

This way you can also do an ngInclude for a login form, or another kind of popup.

UPDATE 2: same last example, but with another solution:

<div ng-if="showMessage">
  <div ng-include="templatePath"/>
</div>

then you can give in the controller the whole path to the partial:

$scope.templatePath = 'alert.html';
like image 197
Polmonite Avatar answered Dec 28 '22 07:12

Polmonite