Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$window.alert not working inside angular function

I want to popup an alert message from an angularJS function, but it's not working.

Script

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.");
    }
};});

What is wrong with this code?

like image 234
sanjeewa Avatar asked Dec 14 '22 22:12

sanjeewa


2 Answers

$window needs to be injected.

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.");
    }
};});

Just replace this code.

like image 183
Narendra Solanki Avatar answered Dec 21 '22 11:12

Narendra Solanki


Only alert("User name or password wrong. please enter correct username or password.") will be suffice.

There is no need of window or $window.

like image 45
Yash Kochar Avatar answered Dec 21 '22 09:12

Yash Kochar