0

My code is as under:

HTML

<section ng-controller="randController as rand">
      Skriv inn minimumtall:<br>
      <input ng-model="rand.minimum" type="number" placeholder="Minste tall"><br>
      <div class="tiny"></div>
      Skriv inn makstall:<br>
      <input ng-model="rand.maksimum" type="number" placeholder="Største tall"><br><br>
      <ul class="nav nav-pills" ng-controller="clickController as click">
          <li ng-click="click.randClick()" ng-class="{'active' : click.randCheck(true)}" >
              <a ng-click="rand.tilfeldig()">Randomize!</a>
          </li>
      </ul>
      <div class="tiny"></div><br>
      <pre>Tilfeldig tall = {{rand.tilfeldig()}}</pre>
</section>

JS

.controller('clickController', function ($timeout) {
  this.randClicked = false;
  this.randClick = function () {
    this.randClicked = true;
    $timeout(function() {
        this.randClicked = false;
    }, 1000, true);
  };
  this.randCheck = function (x) {
      return this.randClicked === x;
  };
})

What I am trying to do is to reset the active class of the <li>-element after about 2 seconds (Unclick it after it has been clicked for 2 seconds). I have gotten tips from forum members but it still doesn't work. Currently, I get this error, which I don't understand.

1

2 Answers 2

2

There is a problem about this pointer in your code. Try this:

.controller('clickController', function ($timeout) {
  var that = this;
  this.randClicked = false;
  this.randClick = function () {
    this.randClicked = true;
    $timeout(function() {
        that.randClicked = false;
    }, 1000, true);
  };
  this.randCheck = function (x) {
      return this.randClicked === x;
  };
})
Sign up to request clarification or add additional context in comments.

5 Comments

This soulotion workes, although it doesn't always work. However it seems very odd to me. If that equals this, why is it a problem to use this?
Update: this soulotion only works when the input boxes are empty. (Couldn't edit last comment because I'm on mobile)
@tjespe this pointer is one of most tricky parts of javascript. It's not quite simple as it looks like. Search some relevant articles and read carefully. It deserve spending 10 mins, I promise.
The button works quite well now, but I wouldn't like the random number to be recalculated 0.2 seconds after the button is clicked. Do you have any idea why that is?
No. It's out of topic of this question. Please post another question.
1

You can use angulars $timeout function for this.

First you have to inject it into your controller like so:

.controller('clickController', function ($timeout) {

Then you can use it like this

this.randClick = function () {
  this.randClicked = true;
  $timeout(function() {
    this.randClicked = false;
  }, 2000)
};

Where 2000 is the amount you want to wait before executing the code inside, in milliseconds

6 Comments

Thanks! I'll try this. But I wonder: why is there a $-sign in front of timeout? Or more generally: what purpose does the $-sign serve in angular?
Generally the $-sign signifies it's an angular function and not native Javascript. $timeout is basically like setTimeout but it will $digest afterwards I believe, so it will update all bindings and such.
@tjespe As per the angular documentation: The $ prefix is there to namespace Angular-provided services. To prevent collisions it's best to avoid naming your services and models anything that begins with a $.
The suggested answer has not solved my problem. The class of the <li>-element stays active. However, the random number output changes after a second (this is not wanted though)
Can you try to add , true after 2000, it shouldn't matter but sometimes its necessary
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.