How do you rotate text values in Angular?

江枫思渺然 提交于 2019-12-25 03:27:28

问题


Completely new to Angular and I can think of 100 different ways to accomplish this outside of Angular, but pushing myself to learn the Angular ways. My goal, I have an element that has plain text in it. I want to rotate that text every x seconds. Here's the base HTML:

<h2>Rotating text in Angular is <span>fun</span></h2>

The span is where I want the text to rotate from fun to 'sucks', 'awesome', 'hard', 'easy' every x number of seconds. A nice transition will also be included, but looking for the best way to implement the functionality using Angular. I've been looking into creating a directive and using Angular's interval, but not quite getting it.

It would be great if all the possible values could be included in the HTML, but I'm open to suggestions for the best way to do this.


回答1:


Check this plunk I made:

rotating text in angularjs

Let's define a word array:

scope.wordArr=['fun','sucks', 'awesome', 'hard', 'easy'];

The directive

<span rotate-text></span>

rotates the words from the array every sec inside the span.

function updateWord(i) {
    var j = (i + 1) % (scope.wordArr.length); // (i + 1) to start at second word
    //so the j rotates like: 1, 2, 3, 4, 0, 1, 2,...
    element.text(scope.wordArr[j]); //changes text inside span
}

element.text(scope.wordArr[0]); // displays "fun"
stopWord = $interval(updateWord, 1000); //executes 'updateWord' every second

As the $interval only starts working after the delay specified, we need to display the 1st word of the array outside the $interval, like so:

element.text(scope.wordArr[0]); //displays "fun"

Hence the need to start indexes in the $interval function at 1, not 0, by using (i + 1) instead of (i), like so:

var j = (i + 1) % (scope.wordArr.length);



回答2:


Manipulating text in angular is quite straight-forward; The best way to accomplish this is to use the controller of the 'ngModel' which we usually call 'ngModelCtrl.' By creating a custom directive and telling it to require an 'ngModel' directive, you have access to this special controller which gives you an API to manipulate the text value of that 'ngModel.'

Here's the Plunker: http://plnkr.co/edit/I2HvpHn5AnnCCe8rtQOW

index.html

  <body ng-app        = "YourAppName"
        ng-controller = "YourAppCtrl">
    <h1>Hello Plunker!</h1>
    <h2>Rotating text in Angular is <span ng-model = "currentAdjective" rotate-text > {{ currentAdjective }} </span></h2>
  </body>

script.js

angular.module('YourAppName', []);

angular.module('YourAppName')
       .controller('YourAppCtrl', function($scope) {
         $scope.currentAdjective;
       })
;


angular.module('YourAppName')
       .directive('rotateText', function($interval) {
           return {
               require: 'ngModel',
               link: function($scope, $elem, $attrs, ngModelCtrl) {
                   var adjectivesToRotate = ["sucks", "hard", "awesome", "easy"];
                   var lengthOfAdjectives = adjectivesToRotate.length;
                   var randomIndex        = Math.floor(Math.random() * (lengthOfAdjectives));

                   var beginInterval = $interval(function() {                  
                     ngModelCtrl.$setViewValue(adjectivesToRotate[randomIndex]);
                     randomIndex = Math.floor(Math.random() * (lengthOfAdjectives));
                   }, 1000);
           }
       };

   })
;


来源:https://stackoverflow.com/questions/28570207/how-do-you-rotate-text-values-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!