Yifan Yifan - 5 months ago 9
AngularJS Question

Changing pages on button click (AngularJS)?

Following online examples, I have something like this in

login.html
:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js"></script>
<script type="text/javascript" src="app/app.login.js"></script>

<div id="loginButton" ng-app="loginButton" ng-controller="loginControl">
<button ng-click="enterSite()">
Enter
</button>
</div>


Then in
app.login.js
:

var appButton = angular.module("loginButton", []);

appButton.controller("loginControl", ["$scope", "$window", function($scope, $window){
$scope.enterSite = function(){
$window.location.href = "../index.html";
}
}]);


However, it does not redirect to
index.html
when I press the button. There are no errors in the browser's log. I've copied most of the format from working examples, so I cannot find why it does not work for me.

Any tips would be appreciated.

Answer

Your code works as it is without any change.

Look in your local system if you see any javascript related issues or script isn't loading from ajax.googleapis.com.

Check if you have linked your login.js properly

var appButton = angular.module("loginButton", []);

appButton.controller("loginControl", ["$scope", "$window", function($scope,  $window){
    $scope.enterSite = function(){
        $window.location.href = "../index.html";        
    }
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js"></script>
<script type="text/javascript" src="app/app.login.js"></script>

<div id="loginButton" ng-app="loginButton" ng-controller="loginControl">
    <button ng-click="enterSite()">
        Enter
    </button>
</div>

Comments