2016-06-11 78 views
0

继网上的例子,我有login.html是这样的:更改点击按钮(AngularJS)上的页面?

<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> 

然后在app.login.js

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

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

然而,当我按下按钮它不重定向到index.html。浏览器日志中没有错误。我从工作示例中复制了大部分格式,所以我找不到为什么它不适用于我。

任何提示将不胜感激。

回答

1

您的代码的工作原样,没有任何改变。

如果您发现任何与JavaScript相关的问题或脚本未从ajax.googleapis.com加载,请查看您的本地系统。

检查,如果你已经将自己的login.js正确

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>

+0

你说得对,我提供了工作的摘录。然而,我很尴尬地在login.html中使用了2 ng-app(经过大量研究后,我才发现你只能使用一个),所以我想它只是忽略了两者。感谢您通知我错误不在我提供的内容中。 – Yifan

+0

@Yifan发生了:)。要加载第二个应用程序,您必须通过代码手动引导它。 –