$ window.alert在angular函数内部不起作用

时间:2017-06-02 05:43:58

标签: javascript angularjs

我想从angularJS函数中弹出警告消息,但它无效。

脚本

app.controller("APIController", function ($scope, $http) {
$scope.saveSubs = function () {
    var sub = {
        UserName: $scope.username,
        Password: $scope.password
    };
    if ($scope.username === 'admin' && $scope.password === 'admin') {
        window.location.href = '/Home/HotelSearchRedirect';
    } else {
        $window.alert("User name or password wrong. please enter correct username or password.");
    }
};});

此代码有什么问题?

4 个答案:

答案 0 :(得分:3)

$window需要注入。

app.controller("APIController", function ($scope, $http,$window) {
$scope.saveSubs = function () {
    var sub = {
        UserName: $scope.username,
        Password: $scope.password
    };
    if ($scope.username === 'admin' && $scope.password === 'admin') {
        window.location.href = '/Home/HotelSearchRedirect';
    } else {
        $window.alert("User name or password wrong. please enter correct username or password.");
    }
};});

只需替换此代码即可。希望这会对你有所帮助。

答案 1 :(得分:3)

仅提醒("用户名或密码错误。请输入正确的用户名或密码。")就足够了。

不需要窗口或$ window。

答案 2 :(得分:1)

您应该使用 window 代替 $window

  window.alert("User name or password wrong. please enter correct username or password.");

答案 3 :(得分:1)

注入$window

app.controller("APIController", function ($window, $scope, $http) { 
   // Code
});