简体   繁体   English

AngularJS ng-包括ng-click

[英]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. 我想找到一种方法将HTML(为控制器优化)插入警报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. 这两个模板应该由JS本身或用户引发。 Those templates should get into this div, but I can't use innerHTML since in templates are some ng-models and such things... 这些模板应该进入这个div,但是我不能使用innerHTML因为在模板中有一些ng模型和类似的东西......

<div id="alert" data-ng-click="empty()" data-ng-controller="alert" role="navigation"></div>

Usually what I do is use ng-if / ng-show . 通常我所做的是使用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. 这样,当$scope.message为空时你的div是空的,你可以自动显示它,只给$scope.message一个值。 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: 如果你需要一个ng-include ,最简单的方法就是在你需要它时显示的div中使用它:

<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; 更新:在我的上一个例子之后,如果你想为每种情况包含不同类型的消息,你可以使用多个ngIf ,包括不同的模板; 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. 这样,您还可以为登录表单或其他类型的弹出窗口执行ngInclude

UPDATE 2: same last example, but with another solution: 更新2:最后一个例子,但另一个解决方案:

<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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM