来自控制器的角度路由和绑定数据

时间:2021-12-07 13:54:02

I have my angular routing like th below code

我有像下面这样的角路由。

var app = angular.module('mainApp', ['ngRoute']);

app.config(function ($routeProvider) {

    $routeProvider.when('/', {
        templateUrl: '/DeclarationForms/V1/EmployeeProfile.html',
        controller: 'empController'
    }).when('/DeclarationAndIndemnityBond.html', {
        templateUrl: '/DeclarationForms/V1/DeclarationAndIndemnityBond.html',
        controller: 'declarationController'
    }).otherwise({
        redirectTo: "/"
    });

    app.controller('empController', function ($scope, $http) {

        var resultPromise = $http.get("../ViewForms/GetData/", { params: { ProcName: "SP_EmployeeProfile_GetList" } });
        resultPromise.success(function (data) {
            console.log(data);
            $scope.employeeProfile = data;
        });
    });
});

The empController calls my controller action with a parameter as per the code

empController根据代码调用带有参数的控制器动作

$http.get("../ViewForms/GetData/", { params: { ProcName: "SP_EmployeeProfile_GetList" } });

The controller's action code is as follows

控制器的动作代码如下。

        [HttpGet]
        [AllowAnonymous]
        public ActionResult GetData(string ProcName)
        {
            if(Session["UserJDRECID"]==null || Session["UserJDRECID"].ToString()=="0")
            {
                return RedirectToAction("Login", "User_Login");
            }
            else
            {
                var UsrJDID = Convert.ToInt32(Session["UserJDRECID"]);
                DataSet dt = Helper.PopulateData(ProcName, UsrJDID);
                string JSONString = string.Empty;
                JSONString = JsonConvert.SerializeObject(dt);
                return Json(JSONString, JsonRequestBehavior.AllowGet);
            }
        }

The form get loaded properly as per the code

按照代码正确地加载表单

templateUrl: '/DeclarationForms/V1/EmployeeProfile.html',

but it don't call my action GetData from where I suppose to bind the EmployeeProfile.html

但是它不会调用我的动作GetData,我要从那里绑定EmployeeProfile.html

If I change my angular controller like below code this still don't work

如果我像下面的代码那样改变我的角控制器,这还是不行

app.controller('empController', function ($scope) 
{
 console.log("hi"); alert(); 
});

My console gives below error

我的控制台给出了以下错误。

Error: error:areq

错误:错误:areq

Bad Argument

坏的论点

Please help me I stuck here.

请帮帮我,我被困在这里了。

Thanks in advance.

提前谢谢。

2 个解决方案

#1


1  

You can't use "../" inside your $http.get.

I don`t know how your project is setup, but you can try:

你不能使用“. ./”在你http.get美元。我不知道你的项目是如何建立的,但是你可以试试:

$http.get("/ViewForms/GetData/", { params: { ProcName: "SP_EmployeeProfile_GetList" } });

In that case the ViewForms is the name of your controller and it needs to be in the root or pass the complete url.

Make sure you are passing all the folders then Controller then your action.

在这种情况下,ViewForms是控制器的名称,它需要位于根目录中,或者传递完整的url。确保您正在传递所有文件夹,然后是Controller,然后是您的操作。

Example: http://www.dotnetcurry.com/angularjs/1202/angular-http-service-aspnet-mvc

例如:http://www.dotnetcurry.com/angularjs/1202/angular-http-service-aspnet-mvc

#2


0  

I change my code as follows and this worked for me.

我将代码更改为如下所示,这对我很有效。

var app = angular.module('mainApp', ['ngRoute']);

app.config(function ($routeProvider) {
    $routeProvider.
        when('/', {
            templateUrl: '/DeclarationForms/V1/EmployeeProfile.html',
            controller: 'empController'
        })
        otherwise({
            redirectTo: "/"
    });
});

app.controller('empController', ['$scope', '$http', EmployeeProfile]);
function EmployeeProfile($scope, $http) {
    $http.get("../ViewForms/GetData", { params: { ProcName: "SP_EmployeeProfile_GetList" } })//Done
        .then(function (response) {
            var mydata = $.parseJSON((response.data));
            $scope.employeeProfile = $.parseJSON(mydata);
    });
}

#1


1  

You can't use "../" inside your $http.get.

I don`t know how your project is setup, but you can try:

你不能使用“. ./”在你http.get美元。我不知道你的项目是如何建立的,但是你可以试试:

$http.get("/ViewForms/GetData/", { params: { ProcName: "SP_EmployeeProfile_GetList" } });

In that case the ViewForms is the name of your controller and it needs to be in the root or pass the complete url.

Make sure you are passing all the folders then Controller then your action.

在这种情况下,ViewForms是控制器的名称,它需要位于根目录中,或者传递完整的url。确保您正在传递所有文件夹,然后是Controller,然后是您的操作。

Example: http://www.dotnetcurry.com/angularjs/1202/angular-http-service-aspnet-mvc

例如:http://www.dotnetcurry.com/angularjs/1202/angular-http-service-aspnet-mvc

#2


0  

I change my code as follows and this worked for me.

我将代码更改为如下所示,这对我很有效。

var app = angular.module('mainApp', ['ngRoute']);

app.config(function ($routeProvider) {
    $routeProvider.
        when('/', {
            templateUrl: '/DeclarationForms/V1/EmployeeProfile.html',
            controller: 'empController'
        })
        otherwise({
            redirectTo: "/"
    });
});

app.controller('empController', ['$scope', '$http', EmployeeProfile]);
function EmployeeProfile($scope, $http) {
    $http.get("../ViewForms/GetData", { params: { ProcName: "SP_EmployeeProfile_GetList" } })//Done
        .then(function (response) {
            var mydata = $.parseJSON((response.data));
            $scope.employeeProfile = $.parseJSON(mydata);
    });
}