【问题标题】:Post method using Asp.Net WebApi controller with AngularJS使用带有 AngularJS 的 Asp.Net WebApi 控制器的发布方法
【发布时间】:2017-05-22 22:58:46
【问题描述】:

Angular 控制器代码:

(function ()
{
myApp.controller("LoginController", function ($scope, $http, $location, $window) {
    $scope.Emp = {};
    console.log("cont");

    $scope.Submit = function (Emp) {
        console.log("inside", $scope.Emp);
        $http({
            method: "POST",
            url: '#/Test/Login',
            data: $scope.Emp,
        })
        .then(function successCallback(response) {
            console.log("response", response.data);
            //$window.location.href = '/Home/Display';
            if (response.data == "CORRECT UserName and Password") {
                console.log("if condition")
                $window.location.href = '/Test/Display';
            }
            else if (response.data == "INCORRECT UserName") {
                alert("INCORRECT UserName or Password");
            }
        })
    }
    $scope.Register = function () {
        $window.location.href = '/Test/Register';
    }
});
})(angular.module('myApp'));

Angular 模块代码:

var myApp = angular.module('myapp', ['ngRoute']);
app.config('$StateProvider', '$UrlRouteProvider','$scope', '$LogProvider', '$http', '$location', '$window',
function ($StateProvider, $UrlRouteProvider, $scope, $http, $location, $window)
{
    $UrlRouteProvider.otherwise("/Login");

    $StateProvider
    .state('Login'),{
        url:'/Login',
        views:{
            '@':{
                templateUrl: "/Views/Home/Login.cshtml",
                controller: 'LoginController'
            }
        }
    }})

【问题讨论】:

标签: angularjs asp.net-web-api


【解决方案1】:

您只声明但没有$scope 之后的配置中注入$LogProvider。声明和注入必须相同且顺序准确。

应该是这样的:

app.config('$StateProvider', '$UrlRouteProvider', '$scope', '$LogProvider', '$http', '$location', '$window',
function ($StateProvider, $UrlRouteProvider, $scope, $LogProvider, $http, $location, $window)
{
    $UrlRouteProvider.otherwise("/Login");

    $StateProvider
    .state('Login'),{
        url:'/Login',
        views:{
            '@':{
                templateUrl: "/Views/Home/Login.cshtml",
                controller: 'LoginController'
            }
        }
    }})

您的url: '#/Test/Login', 中还有#,请检查您是否需要它。

此外,根据您的代码,您似乎不需要在 config.在您的代码中,我没有看到任何使用 $scope, $LogProvider, $http, $location, $window..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 2017-10-30
    相关资源
    最近更新 更多