无法通过ID和从阿贾克斯或者AngularJS呼叫控制器 [英] Can't pass ID and call Controller from ajax or AngularJS
问题描述
我有对象的表,我想通过自己的 ID
上点击,并没有刷新填充股利或表,我试图使用AJAX和角但不能通过 ID
。
如果我固定在它工作的具体数量的 ID
。
在查看:
<脚本>
功能CountryController($范围,$ HTTP){
$ http.get(/ API / SportApi / GetCountry /)。成功(函数(响应){
$ scope.controllerFunction =功能(值){
执行console.log(值);
}
物镜= JSON.parse(响应);
$ scope.country = angular.fromJson(OBJ);
});
}
< / SCRIPT>
<一类=项项缩略图左NG-HREF =#/ API / SportApi / GetCountry / {{x.id}}NG点击=CountryControler(x.id)>
点击国家
&所述; / a取代;
在你看来,你应该有:
<一类=项项缩略图左的href NG点击=CountryControler(x.id)>点击为国家< / A>
在这里的href将停止链接,从重新加载页面和NG-点击将调用一个方法在控制器上。 在你的控制器,你应该定义的范围变量函数:
VAR对myApp = angular.module('对myApp',[]);
myApp.controller('ExamleController',['$范围',函数($范围){
$ scope.CountryController =功能(ID){
$ http.get(/ API / SportApi / GetCountry /+ ID).success(函数(响应){
$ scope.controllerFunction =功能(值){
执行console.log(值);
}
物镜= JSON.parse(响应);
$ scope.country = angular.fromJson(OBJ);
});
}
}]);
验证与纳克定义HTML的根元素,您已经自举角JS( -app </A>)。
验证(通过的 NG-控制器)。
您也应该检查你得到正确的ID上的观点称之为x.id的时候。
$ scope.controllerFunction似乎没有必要在这种情况下
I have a table of objects, and I want to pass their ID
on click, and without refresh populate a div or table, I've tried with AJAX and angular but can not pass ID
.
If I fix the ID
on a specific number it works.
In the View:
<script>
function CountryController($scope, $http) {
$http.get("/api/SportApi/GetCountry/").success(function (response) {
$scope.controllerFunction = function (value) {
console.log(value);
}
obj = JSON.parse(response);
$scope.country = angular.fromJson(obj);
});
}
</script>
<a class="item item-thumbnail-left" ng-href="#/api/SportApi/GetCountry/{{x.id}}" ng-click="CountryControler(x.id)">
click for countries
</a>
On your view, you should have:
<a class="item item-thumbnail-left" href ng-click="CountryControler(x.id)">click for countries</a>
where href will stop the link from reloading the page and the ng-click will call a method on your controller. In your controller, you should define that function on the scope variable:
var myApp = angular.module('myApp',[]);
myApp.controller('ExamleController', ['$scope', function($scope) {
$scope.CountryController = function(id) {
$http.get("/api/SportApi/GetCountry/" + id).success(function (response) {
$scope.controllerFunction = function (value) {
console.log(value);
}
obj = JSON.parse(response);
$scope.country = angular.fromJson(obj);
});
}
}]);
Verify that you have bootstrapped angular js (by defining the root element of your html with ng-app).
Verify that you have attached the controller class to your view (by defining the controller html element with ng-controller).
You should also verify that you are getting the right id on the view when calling "x.id".
$scope.controllerFunction seems unnecessary in this case.
这篇关于无法通过ID和从阿贾克斯或者AngularJS呼叫控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!