![](/img/trans.png)
[英]AngularJS “ReferenceError: window is not defined” in NodeJS Application
[英]AngularJS ReferenceError: $window is not defined
如果他們通過我的表單驗證(根據數據庫值檢查用戶名和密碼),我試圖重新指導我的用戶。
驗證工作正常,但在我的.Success函數中,重定向似乎不起作用,它產生錯誤:'ReferenceError:$ window not defined'。
這是代碼:
.success(function(data) {
console.log(data);
if (!data.success) {
// if not successful, bind errors to error variables
$scope.errorUserName = data.errors.userName;
$scope.errorUserPassword = data.errors.userPassword;
} else {
// if successful, bind success message to message
$scope.message = data.message;
$window.location=('twitter.com');
}
});
我試過改變位置路徑,但似乎沒有任何工作。 有任何想法嗎?
謝謝!
LazyTotoro
$window
需要注入。
要注入它,只需將其作為參數添加到控制器功能中,Angular將自動處理其余部分。
例如:
app.controller('MyController', function MyController($scope, $window) {
$window.location = 'http://stackoverflow.com'
});
您可以在此處閱讀有關AngularJS中依賴項注入的更多信息。
如果您不需要重新加載整頁,則應該注入並使用$ location :
// get the current path
$location.path();
// change the path
$location.path('/newValue');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.