AngularJs 指令问题
AngularJs Directive Issue
app.directive('ensureUnique', ['$http', function ($http) {
return {
require: 'ngModel',
link: function (scope, ele, attrs, c) {
scope.$watch(attrs.ngModel, function () {
$http.post("http://localhost:xxxx/api/Persons/CheckUserName", c.$modelValue).success(function (data) {
c.$setValidity('unique', false);
}).error(function () {
c.$setValidity('unique', false);
});
});
}
}
}]);
服务器:
[HttpPost]
public bool CheckUserName(string field)
{
bool IsUnique = false;
if(field=="Meee")
{
IsUnique = false;
}
else
{
IsUnique = true;
}
return IsUnique;
}
我正在尝试验证输入的用户不存在。我正在使用 VS 2015。好的,我的问题是我在 CheckUserName API.Its 中有一个断点没有命中!当我尝试访问不同的 API 断点是否像往常一样命中?
angular
.module('app')
.controller('Main', function ($scope, $http) {
$scope.GetPersons = function () {
$http.get("http://localhost:xxxx/api/Persons/GetPersons").success(function (data) {
$scope.Persons = data;
}).error(function () {
alert("failed");
});
},
$scope.PostPersons = function () {
$http.post("http://localhost:xxxx/api/Persons/GetPersonsWithAge", $scope.Persons).success(function (data) {
$scope.PersonsWAges = data;
alert("Success");
}).error(function () {
alert("failed");
});
}
});
注意:这是两个不同的项目,但都是从调试开始的。
当您尝试将参数传递给 post 方法时,您需要在 $http.post
第二个参数中以数据 JSON
格式传递参数。这需要在您使用 .Net MVC 时进行字符串化。
代码
$http.post("http://localhost:xxxx/api/Persons/CheckUserName", JSON.stringify({
field: c.$modelValue
}))
app.directive('ensureUnique', ['$http', function ($http) {
return {
require: 'ngModel',
link: function (scope, ele, attrs, c) {
scope.$watch(attrs.ngModel, function () {
$http.post("http://localhost:xxxx/api/Persons/CheckUserName", c.$modelValue).success(function (data) {
c.$setValidity('unique', false);
}).error(function () {
c.$setValidity('unique', false);
});
});
}
}
}]);
服务器:
[HttpPost]
public bool CheckUserName(string field)
{
bool IsUnique = false;
if(field=="Meee")
{
IsUnique = false;
}
else
{
IsUnique = true;
}
return IsUnique;
}
我正在尝试验证输入的用户不存在。我正在使用 VS 2015。好的,我的问题是我在 CheckUserName API.Its 中有一个断点没有命中!当我尝试访问不同的 API 断点是否像往常一样命中?
angular
.module('app')
.controller('Main', function ($scope, $http) {
$scope.GetPersons = function () {
$http.get("http://localhost:xxxx/api/Persons/GetPersons").success(function (data) {
$scope.Persons = data;
}).error(function () {
alert("failed");
});
},
$scope.PostPersons = function () {
$http.post("http://localhost:xxxx/api/Persons/GetPersonsWithAge", $scope.Persons).success(function (data) {
$scope.PersonsWAges = data;
alert("Success");
}).error(function () {
alert("failed");
});
}
});
注意:这是两个不同的项目,但都是从调试开始的。
当您尝试将参数传递给 post 方法时,您需要在 $http.post
第二个参数中以数据 JSON
格式传递参数。这需要在您使用 .Net MVC 时进行字符串化。
代码
$http.post("http://localhost:xxxx/api/Persons/CheckUserName", JSON.stringify({
field: c.$modelValue
}))