【发布时间】:2014-09-26 19:15:11
【问题描述】:
我是 AngularJS 的新手。我想从我的角度控制器向我的 MVC5 控制器进行 ajax 调用。我的 action 带有 三个参数 驻留在 HomeController 中,例如
public ActionResult addFrnd(int name, string phone, int age)
{
//do something
return json();
}
我的角度控制器放在这里
var myModule = angular.module("myApp", ['ngRoute']);
myModule.controller('SimpleController', function ($scope, $http, SimpleFactory) {
$scope.friends = [];
init();
function init() {
$scope.friends = SimpleFactory.getFriend();
}
$scope.addFriend = function ($http) {
$scope.friends.push(
{
name: $scope.newFriend.name,
phone: $scope.newFriend.phone,
age: $scope.newFriend.age
});
//$http("/Home/addFrnd");
$http.get({
type: "POST",
url: "~/Home/addFrnd",
data: {
name: $scope.newFriend.name,
phone: $scope.newFriend.phone,
age: $scope.newFriend.age
},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);
},
error: function (msg) {
alert(msg.d);
}
});
};
});
我该如何解决这个问题?请帮帮我
【问题讨论】:
-
为什么要将 $http 服务传递给 addFriend 函数?