0

I am very new to angular js and want to make a very simple website, that does the following: when clicking on a button, it will randomly generate a number, and if the number is odd, redirect to google, otherwise redirect to apple website. I have done the following:

<!doctype html>
<html lang="en" ng-app="homeapp">
<head> ... all imports </head>
<body ng-controller="homeappCtrl"> 
  <a href="{{urlToPick}}">Click</a>
</body>
</html>

and then in the controller.js, I have:

var homeapp= angular.module('homeappController', []);

homeappController.controller('homeappCtrl', ['$scope',
  function($scope) {
    if (Math.floor((Math.random() * 10) + 1)%2==0)
      {$scope.urlToPick = 'http://google.ca';}
    else
      {$scope.urlToPick = 'http://apple.com';}
  }]);

The problem is that when I click on the link, the urlToPick is not resolved and therefore the link returns error. (the link url is something like xxx/{{urlToPick}} )

I guess I have done something wrong but please tell me where... Thanks!

2
  • what is problem here? Commented Jun 11, 2014 at 5:07
  • I strongly recommend you to use ng-href when generates links dynamically... you can face some problems if your html is generated before your link... Commented Jun 11, 2014 at 5:47

1 Answer 1

1

Your ng-app and your angular.module definitions are different.

Change

var homeapp= angular.module('homeappController', []);

to

var homeapp= angular.module('homeapp', []);
Sign up to request clarification or add additional context in comments.

Comments

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.