我试图通过复选框和搜索字段过滤数据。当我将静态json数组提供给$scope.students
变量时,代码工作正常,但是当我使用$http.get("data.php").then(function(response) {...});
<我通过php文件获取相同的json数组时它没有显示任何内容/ p>
<div ng-app="app" ng-controller="ctrl">
<input type="text" ng-model="findname" placeholder="Search Name">
<ul>
<li ng-repeat="tech in technologyArray">
<label>
<input type="checkbox" ng-model="tech.on">{{tech.u_proffession}}
</label>
</li>
</ul>
<ul>
<li ng-repeat="stu in students | filter:{u_name: findname} | customFilter:(technologyArray|filter:{on:true})">
<strong>Name :{{stu.u_name}}</strong> ({{stu.u_proffession}})
</li>
</ul>
</div>
angular.module('app', [])
.controller('ctrl', function($scope) {
$scope.technologyArray = [{
u_proffession: "developer",
on: false
}, {
u_proffession: "driver",
on: false
}];
$scope.students = [
{"u_name":"adrian","u_mail":"Aenean.sed@ut.net","u_images":"11.jpg","u_proffession":"developer"},
{"u_name":"shelley","u_mail":"urna@In.ca","u_images":"11.jpg","u_proffession":"developer"},
{"u_name":"nasim","u_mail":"sed@acurna.edu","u_images":"11.jpg","u_proffession":"driver"}
];
})
.filter('customFilter', function() {
return function(input, techs) {
if(!techs || techs.length === 0) return input;
var out = [];
angular.forEach(input, function(item) {
angular.forEach(techs, function(tech) {
if (item.u_proffession === tech.u_proffession) {
out.push(item);
}
});
});
return out;
}
});
上面的代码工作正常,但我想从PHP文件中获取数据,现在它使用静态json数据。因此,当我添加下面的脚本而不是静态json数组时,它就无法正常工作。
controller('ctrl', function($scope) {
....
$http.get("data.php").then(function(response) {
$scope.students= response.data.records
});
})
data.php
{"records":[{"u_name":"adrian","u_mail":"abc@gmail.net","u_proffession":"developer"},{...}]}
任何人都可以指导我如何解决问题。如果有人指导我,我会很感激。谢谢。