Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs MVC中使用Angular的Webservice调用_Angularjs - Fatal编程技术网

Angularjs MVC中使用Angular的Webservice调用

Angularjs MVC中使用Angular的Webservice调用,angularjs,Angularjs,有人能解释一下在MVC项目中如何使用Angular调用动作吗 我成功地使用Ajax调用了如下操作: var app = angular.module('toprightSec', ['ng']); app.controller('NationalityCtrl', ['$scope', function ($scope, $http) { $scope.items = []; var items = populateListFromLocalStorage("offices", "Login/

有人能解释一下在MVC项目中如何使用Angular调用动作吗

我成功地使用Ajax调用了如下操作:

var app = angular.module('toprightSec', ['ng']);
app.controller('NationalityCtrl', ['$scope', function ($scope, $http) {
$scope.items = [];

var items = populateListFromLocalStorage("offices", "Login/GetOffices", 24);
var officelist = "";

for (var i = 0; i < items.length; i++)
{
    $scope.items[i] = { "name": read_prop(items[i], 'office_desc'), "guid": read_prop(items[i], 'office_guid') };
}

$scope.reloadPage = function () { window.location.reload(); }

$scope.getResult = function ($index, item) {

    $.ajax({
            type: 'GET',
            async: true,
            url: 'Login/ChangeOffice',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: {
                officeID: $scope.items[$index].guid,
                officeName: $scope.items[$index].name,
            },
            success: function (msg) {
            }
        });
};

}]);
var AngularModule = angular.module('toprightSec', ['ng']);

AngularModule.service('ApiCall', ['http', function ($http) {

var result;
this.PostApiCall = function (controllerName, methodName, obj) {
    debugger;
    result = $http.post('api/' + controllerName + '/' + methodName,obj).success(function (data, success) {
        result = (data);
    }).error(function () {
        ("Something went wrong");
    });
    return result;
};

}]);

AngularModule.controller('NationalityCtrl', ['$scope', function ($scope, $http, ApiCall) {
$scope.items = [];
var items = populateListFromLocalStorage("offices", "Login/GetOffices", 24);
var officelist = "";
for (var i = 0; i < items.length; i++)
{
    $scope.items[i] = { "name": read_prop(items[i], 'office_desc'), "guid": read_prop(items[i], 'office_guid') };
}

$scope.reloadPage = function () { window.location.reload(); }

$scope.getResult = function ($index, item) {

    var obj = {
        'officeID' : '123',
        'officeName' : 'Sample'
    }

    var result = ApiCall.PostApiCall("Login", "ChangeOffice", obj).success(function (data) {

        var data = $.parseJSON(JSON.parse(data));
        $scope.message = data;

    });

};

}]);
var-app=angular.module('toprightSec',['ng']);
app.controller('NationalityCtrl',['$scope',函数($scope,$http){
$scope.items=[];
var items=populateListFromLocalStorage(“offices”,“Login/GetOffices”,24);
var officelist=“”;
对于(变量i=0;i
我试着把它改成这样的角度:

var app = angular.module('toprightSec', ['ng']);
app.controller('NationalityCtrl', ['$scope', function ($scope, $http) {
$scope.items = [];

var items = populateListFromLocalStorage("offices", "Login/GetOffices", 24);
var officelist = "";

for (var i = 0; i < items.length; i++)
{
    $scope.items[i] = { "name": read_prop(items[i], 'office_desc'), "guid": read_prop(items[i], 'office_guid') };
}

$scope.reloadPage = function () { window.location.reload(); }

$scope.getResult = function ($index, item) {

    $.ajax({
            type: 'GET',
            async: true,
            url: 'Login/ChangeOffice',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: {
                officeID: $scope.items[$index].guid,
                officeName: $scope.items[$index].name,
            },
            success: function (msg) {
            }
        });
};

}]);
var AngularModule = angular.module('toprightSec', ['ng']);

AngularModule.service('ApiCall', ['http', function ($http) {

var result;
this.PostApiCall = function (controllerName, methodName, obj) {
    debugger;
    result = $http.post('api/' + controllerName + '/' + methodName,obj).success(function (data, success) {
        result = (data);
    }).error(function () {
        ("Something went wrong");
    });
    return result;
};

}]);

AngularModule.controller('NationalityCtrl', ['$scope', function ($scope, $http, ApiCall) {
$scope.items = [];
var items = populateListFromLocalStorage("offices", "Login/GetOffices", 24);
var officelist = "";
for (var i = 0; i < items.length; i++)
{
    $scope.items[i] = { "name": read_prop(items[i], 'office_desc'), "guid": read_prop(items[i], 'office_guid') };
}

$scope.reloadPage = function () { window.location.reload(); }

$scope.getResult = function ($index, item) {

    var obj = {
        'officeID' : '123',
        'officeName' : 'Sample'
    }

    var result = ApiCall.PostApiCall("Login", "ChangeOffice", obj).success(function (data) {

        var data = $.parseJSON(JSON.parse(data));
        $scope.message = data;

    });

};

}]);
var AngularModule=angular.module('toprightSec',['ng']);
AngularModule.service('ApiCall',['http',function($http){
var结果;
this.PostApiCall=函数(controllerName、methodName、obj){
调试器;
结果=$http.post('api/'+controllerName+'/'+methodName,obj)。成功(函数(数据,成功){
结果=(数据);
}).错误(函数(){
(“出了问题”);
});
返回结果;
};
}]);
控制器('NationalTycTrl',['$scope',函数($scope,$http,ApiCall){
$scope.items=[];
var items=populateListFromLocalStorage(“offices”,“Login/GetOffices”,24);
var officelist=“”;
对于(变量i=0;i
我一直发现这个错误“PostApiCall”没有在浏览器控制台上定义

知道我做错了什么吗


谢谢。

用户承诺,完成$http后返回:

    this.PostApiCall = function (controllerName, methodName, obj) {
        debugger;
        var deferred = $q.defer();
        $http.post('api/' + controllerName + '/' + methodName,obj).success(function (data) {
            deferred.resolve(data);
        });
        return deferred.promise;
    };

    var result = ApiCall.PostApiCall("Login", "ChangeOffice", obj).then(function (data) {
        var data = $.parseJSON(JSON.parse(data));
        $scope.message = data;
    });

嗯,我设法解决了它,我注意到我的代码有两个问题:

  • 我在两个不同的地方使用了$http,在这两个地方我都使用了success。(charlietfl建议的解决方案)
  • 另一个问题是我传递的参数不正确
  • 这是我更新的工作代码:

    var app = angular.module('BMSApp', []);
    
    app.factory('ApiCall', ['$http', function ($http) {
    var PostApiCall = function (controllerName, methodName) {
        return $http.post(controllerName + '/' + methodName);
    };
    var GetApiCall = function (controllerName, methodName) {
        return $http.get(controllerName + '/' + methodName);
    };
    return {
        PostApiCall: PostApiCall,
        GetApiCall: GetApiCall
    };
    }]);
    
    app.controller('NationalityCtrl', ['ApiCall', '$scope', function (ApiCall,$scope) {         
    $scope.items = [];
    var items = populateListFromLocalStorage("offices", "Login/GetOffices", 24);
    var officelist = "";
    for (var i = 0; i < items.length; i++)
    {
        $scope.items[i] = { "name": read_prop(items[i], 'office_desc'), "guid": read_prop(items[i], 'office_guid') };
    }
    $scope.getResult = function ($index, item) {
        var result = ApiCall.PostApiCall("Login", "ChangeOffice/?officeID=" + $scope.items[$index].guid + "&officeName="+$scope.items[$index].name).then(function (data) {
            $scope.reloadPage();
        });
    };
    }]);
    
    var-app=angular.module('BMSApp',[]);
    app.factory('ApiCall',['$http',函数($http){
    var PostApiCall=函数(controllerName,methodName){
    返回$http.post(controllerName+'/'+methodName);
    };
    var GetApiCall=函数(controllerName,methodName){
    返回$http.get(controllerName+'/'+methodName);
    };
    返回{
    PostApiCall:PostApiCall,
    GetApiCall:GetApiCall
    };
    }]);
    app.controller('nationalyCtrl',['ApiCall','$scope',函数(ApiCall,$scope){
    $scope.items=[];
    var items=populateListFromLocalStorage(“offices”,“Login/GetOffices”,24);
    var officelist=“”;
    对于(变量i=0;i

    谢谢大家的帮助。

    使用angular
    $http
    而不是
    $。ajax
    。阅读文档以查看差异。这不是一个代码转换网站,研究ajax的角度应该很容易,至少你已经找到了一个起点并尝试了一些东西尝试了什么?这里的想法是展示您尝试实现的代码,我们可以帮助您使用该代码。我已经用代码的角度版本更新了我的帖子,您能解释一下我做错了什么吗?使用
    then()
    ,而不是在controller中成功。不能有2个
    success
    ,现在首选使用
    then()
    ,不推荐使用
    success
    ,但尚未成功。