我在Eclipse上有一个简单的Web项目,包含三个主要文件:" controller.js"," home.html"和" array.php"。
.php文件应该回显用JSON字符串编码的php数组:
<?php
$arr = array(array(title => "hello", author => "me"), array(title =>
"hello2", author => "me"));
echo json_encode($arr);
?>
然后,控制器在&#34; controller.js&#34;应该接收这个JSON字符串并将其存储到JavaScript数组中:
angular.module('app_controller')
.controller('control', ['$scope', '$http', function($scope, $http) {
this.read = function() {
$http.get('array.php')
.then(function (response) {
$scope.js_array = response.data;
});
};
this.read();
}])
最后,我将访问&#34; home.html&#34;:
中的数组元素<div data-ng-controller="control as ctrl">
<ul>
<li data-ng-repeat="elem in js_array">
<h2>title: {{ elem.title }}</h2>
</li>
</ul>
</div>
但是......它不起作用。 Chrome控制台没有显示任何错误,但看起来我正在访问一个空数组并且我的标题不会出现。我非常确定我正确编码了JSON,因此我认为我未能将控制器与.php文件连接起来。有什么建议吗?
答案 0 :(得分:0)
<强>样本强>
var myApp = angular.module('myApp',[]);
myApp.controller('control', ['$scope', '$http', function($scope, $http) {
this.read = function() {
this.js_array = [
{
"id": "id1",
"title": "title1"
},
{
"id": "id2",
"title": "title2"
},
{
"id": "id3",
"title": "title3"
}
];
};
this.read();
}]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app="myApp" ng-controller="control as ctrl">
<ul>
<li data-ng-repeat="elem in ctrl.js_array">
<h2>title: {{ elem.title }}</h2>
</li>
</ul>
</div>
&#13;
答案 1 :(得分:0)
感谢所有试图帮助我的人,最后我发现问题是我的GlassFish服务器和Quercus。切换到Apache,现在一切正常!