简体   繁体   English

如何在设定的时间间隔后以角度附加文字?

[英]how to append text in angular after set interval of time?

I make a simple in jquery in which I append the text in div .I am able to that http://jsfiddle.net/naveennsit/rgeHe/ But same thing I need to using angular .js but It is not append the data ..why ?how it is possible ?here is my plunker http://plnkr.co/edit/Vzq8yQdb52fJaE7Odv4X?p=preview 我在jquery中做了一个简单的操作,我将文本附加到div中。我能够http://jsfiddle.net/naveennsit/rgeHe/但是同样的事情我需要使用angular .js但是它不附加数据。 。为什么?怎么可能?这是我的傻瓜http://plnkr.co/edit/Vzq8yQdb52fJaE7Odv4X?p=preview

 <!DOCTYPE html>
    <html>

      <head>
        <link data-require="bootstrap-css@3.x" data-semver="3.2.0" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
        <script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
        <script data-require="ui-bootstrap@0.10.0" data-semver="0.10.0" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.js"></script>
        <link rel="stylesheet" href="style.css" />
        <script src="script.js"></script>
      </head>

      <body ng-app="app">
      <div ng-controller="cntrl">
      <div ng-repeat="i in items">{{i}}</div>
    </div>
      </body>
      <script>
      var app=angular.module('app',[]);
      app.controller('cntrl',function($scope,$interval){
        $scope.items=[];
        var i=0;
        $interval(function(){
          $scope.items.push("Hiii I am div");
          $scope.apply();
        },1000)
      })

      </script>

    </html>

You need to remove scope.$apply() ($timeout will already invoke digest cycle and you will end up getting errors for trying to invoke digest when it is already in progress) from the $timeout and provide a track by on array index otherwise it will fail for duplicate keys since the array contains just the string it will be automatically taken as the key, and using same string will cause duplicate keys. 你需要删除scope.$apply() ($ timeout已经调用了摘要周期,你最终会在$timeout时尝试调用摘要时出现错误)并在数组索引上提供track by它会因重复键而失败,因为数组只包含它将自动作为键的字符串,并且使用相同的字符串将导致重复键。 Another way would be to change it to array of objects which holds the string. 另一种方法是将其更改为包含字符串的对象数组。 Ex:- $scope.items.push({name: "Hiii I am div"}); 例如: - $scope.items.push({name: "Hiii I am div"});

<div ng-controller="cntrl">
  <div ng-repeat="i in items track by $index">{{i}}</div>
</div>

and

var app=angular.module('app',[]);
  app.controller('cntrl',function($scope,$interval){
    $scope.items=[];
    var i=0;
    $interval(function(){
      $scope.items.push("Hiii I am div");
    },1000)
  })

Demo 演示

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

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