使用$ compiled时,循环中的ng-repeat重复itens

马可

我正在尝试将一些内容加载到引导程序的弹出窗口中。此内容是隐藏div的html元素,除其他外,它们具有在对象列表下进行迭代的ng-repeat。好的,它可以工作。但是ng-click在popover内部不起作用。为了解决这个问题,根据我在stackoverflow中阅读的内容,当我将内容设置为popover时,我使用了$ compile选项。是的,它也可以。但是,当我这样做时,对象列表在其中显示为重复。有人可以告诉我怎么了吗?

柱塞

angular.module("app", [])
    .controller("progressBarCtrl", function($scope, $timeout, $compile) {
        $scope.value = 90;
		
		$scope.cursos = [
			{ "nome": "Medicina", "percentual": "20" },
			{ "nome": "Ciência da computação", "percentual": "90" }
			
		];		
	
		$scope.showPopover = function() {
			$("#main").popover("show");			
		}
		
 		$timeout(function() {	
			$scope.$apply(function() {		
			$("#main").popover({
				container: 'body',
				html: true,
				content: function () {
					return $compile($("#popper-content").html())($scope);							
				}
			});	
			});
		});
		
		$scope.showAlert = function() {
			alert();
		};
		
    });
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

<body ng-app="app" ng-controller="progressBarCtrl">
<div class="row">
  <div class="col-md-3 col-md-offset-3" id="main" data-ng-mouseenter="showPopover()" data-placement="bottom" title="Progresso detalhado">
  		<h4>Progresso geral:</h4>
        <div class="progress">
          <div class="progress-bar progress-bar-striped" role="progressbar" aria-valuenow="{{value}}" aria-valuemin="0" aria-valuemax="100" style="width: {{value}}%">
          {{value}}%
          </div>
        </div>
  </div>
</div>

<div id="popper-content" class="hide"  >
    <div data-ng-repeat="curso in cursos">
    	<h3>{{curso.nome}}:</h3>
        <div class="progress">
          <div class="progress-bar progress-bar-striped" role="progressbar" aria-valuenow="{{curso.percentual}}" aria-valuemin="0" aria-valuemax="100" style="width: {{curso.percentual}}%">
          {{curso.percentual}}%
          </div>
        </div>
        
        <div ng-click="showAlert()">click-me</div>       
    </div>
</div>
</body>

凯尔琴弦

答案非常复杂。在后台,重复执行两次。一旦应用程序加载,因为它就位于应用了Angular控制器的元素(主体)内部的DIV中。

为了使这项工作正常进行,您不仅需要使用CSS“隐藏”模板内容,还需要将其隐藏在Angular中。您可以通过放置模板和外部文件(到目前为止,最常见的是实际应用程序)或使用script标签来做到这一点。

另外,在示例代码中,您将作用域应用在$ timeout内;这不是必需的,$ timeout会为您调用它。

请参阅以下插件,我认为它现在可以满足您的需求。

http://plnkr.co/edit/jKtG1W1V9a6MZI5uVPdP?p=preview

最重要的代码:

    $timeout(function() {   
        $("#main").popover({
            container: 'body',
            html: true,
            content: function () {
                return $compile($templateCache.get('popper-content'))($scope);                          
            }
        });
    });

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用ng-repeat循环标记的内部内容,但不重复父项

来自分类Dev

使用ng-repeat-start重复多个<td>元素

来自分类Dev

Angularjs表头不断重复(使用ng-repeat)

来自分类Dev

使用ng-repeat。过滤出重复值

来自分类Dev

使用ng-repeat。过滤出重复值

来自分类Dev

Angularjs表头不断重复(使用ng-repeat)

来自分类Dev

关闭源文件时,无法在for循环中重复使用内容

来自分类Dev

如何在AngularJs中使用ng-repeat重复重复的对象?

来自分类Dev

表排序(使用ng-repeat并使用ng-if删除重复的列名称)

来自分类Dev

在 ng-repeat 循环中从 ng-click 订购元素

来自分类Dev

使用for循环循环时如何避免重复?

来自分类Dev

使用ng-repeat以不同的id和ng-init重复相同的div

来自分类Dev

在for循环中声明i时使用var

来自分类Dev

使用if语句时卡在while循环中

来自分类Dev

Angular2中的Ng-repeat-start-也使用NgFor重复多个元素

来自分类Dev

如何在<ul>标记上使用AngularJS ng-repeat而不重复?

来自分类Dev

循环中重复使用boost :: geometry :: intersection

来自分类Dev

使用random.sample的for循环中无重复

来自分类Dev

重复ng-repeat错误

来自分类Dev

使用ng-repeat时角度倾斜

来自分类Dev

列出子页面时,在foreach循环中重复的Wordpress循环

来自分类Dev

ng-repeat-在html循环中计数

来自分类Dev

AngularJS:嵌套循环中$ index的ng-repeat跟踪

来自分类Dev

嵌套ng-repeat的内部循环中的orderBy

来自分类Dev

如何避免在ng-repeat循环中呈现定义标签?

来自分类Dev

AngularJS + Ionic。Ng重复。在循环中的步骤之间插入元素

来自分类Dev

卡在循环中使用try时执行while循环

来自分类Dev

角度ng-repeat,每个父循环的嵌套重复

来自分类Dev

ng-mouseenter 和 ng-mouseleave 在 ng-repeat 循环中不起作用

Related 相关文章

  1. 1

    如何使用ng-repeat循环标记的内部内容,但不重复父项

  2. 2

    使用ng-repeat-start重复多个<td>元素

  3. 3

    Angularjs表头不断重复(使用ng-repeat)

  4. 4

    使用ng-repeat。过滤出重复值

  5. 5

    使用ng-repeat。过滤出重复值

  6. 6

    Angularjs表头不断重复(使用ng-repeat)

  7. 7

    关闭源文件时,无法在for循环中重复使用内容

  8. 8

    如何在AngularJs中使用ng-repeat重复重复的对象?

  9. 9

    表排序(使用ng-repeat并使用ng-if删除重复的列名称)

  10. 10

    在 ng-repeat 循环中从 ng-click 订购元素

  11. 11

    使用for循环循环时如何避免重复?

  12. 12

    使用ng-repeat以不同的id和ng-init重复相同的div

  13. 13

    在for循环中声明i时使用var

  14. 14

    使用if语句时卡在while循环中

  15. 15

    Angular2中的Ng-repeat-start-也使用NgFor重复多个元素

  16. 16

    如何在<ul>标记上使用AngularJS ng-repeat而不重复?

  17. 17

    循环中重复使用boost :: geometry :: intersection

  18. 18

    使用random.sample的for循环中无重复

  19. 19

    重复ng-repeat错误

  20. 20

    使用ng-repeat时角度倾斜

  21. 21

    列出子页面时,在foreach循环中重复的Wordpress循环

  22. 22

    ng-repeat-在html循环中计数

  23. 23

    AngularJS:嵌套循环中$ index的ng-repeat跟踪

  24. 24

    嵌套ng-repeat的内部循环中的orderBy

  25. 25

    如何避免在ng-repeat循环中呈现定义标签?

  26. 26

    AngularJS + Ionic。Ng重复。在循环中的步骤之间插入元素

  27. 27

    卡在循环中使用try时执行while循环

  28. 28

    角度ng-repeat,每个父循环的嵌套重复

  29. 29

    ng-mouseenter 和 ng-mouseleave 在 ng-repeat 循环中不起作用

热门标签

归档