在angulajs中点击ng-click包含模板

时间:2015-09-21 04:19:57

标签: angularjs templates angularjs-ng-click angularjs-ng-include

我在angularjs中创建了讨论论坛应用程序。一旦用户点击帖子上的回复按钮,我想显示在单独的html文件中的编辑器。如何在单击特定帖子下的ng-click时包含该模板。

2 个答案:

答案 0 :(得分:1)

ng-includeng-if的组合怎么样?

如果要根据特定条件注入模板,可以这样做:

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

其中template.html是您的文件名,yourCondition应该是布尔表达式。

这是一个演示:http://plnkr.co/edit/m50nKigoOWYwuczBIQdQ?p=preview

的plnkr

答案 1 :(得分:0)

如上所述,我们可以结合使用ng-if和ng-include来实现所需的功能:

<html ng-app="includeApp">
<head>
  <script type ="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular.js"></script>
  <script src="AppController.js"></script>
</head>
<body ng-controller="AppCtrl">
 <button ng-click="injectReply()">Include Reply</button>
<div ng-include="'reply.html'" ng-if="isReplyIncluded"></div>
</body>
</html>

现在包含AppController.js的代码:

angular.module('includeApp',[])
  .controller('AppCtrl',['$scope',function($scope){
   $scope.isReplyIncluded=false;
    $scope.injectReply= function (){
   //add reply.html by making this variable true
     $scope.isReplyIncluded=true;
    }
  }]);

Reply.html

<div>
You can add your editor html content here
</div>