0

I'm really new to AngularJs and I can't find how to do the following :

A user is answering questions in a quiz.html page. Each answer is added to a "answers" list. In a controller I want to redirect to a result.html page if my "answers" list contains 4 answers...

if (answers.length === 4) {
    ???? }

I know this is a simple question but I can't find any simple answer Thank you !

Lucas B
  • 2,183
  • 1
  • 22
  • 22

2 Answers2

1

You can use Angular $location

if (answers.length === 'whatever') {
    $location.path("/result")
}

In your app.js

$routeProvider.when('/result', {
    templateUrl: 'templates/result.html',
        controller: 'ResultCtrl'
    })...

Make sure to inject the $location into your controller.

Brian
  • 4,958
  • 8
  • 40
  • 56
0

You could attach a ng-click when a user clicks, so in your controller you would have,

$scope.checkAnswerLength = function(){
    if (answers.length === 4) {
        $location.path("/result")
    }
}

Then in HTML add on click checkAnswerLength, and if the answers length is 4 it will redirect to route /result

Samir Alajmovic
  • 3,247
  • 3
  • 26
  • 28