1
我想從角js做一個ajax請求到php。 我沒有錯誤,並顯示在視圖中沒有數據記錄爲什麼在視圖中顯示沒有數據記錄AngularJS
從服務器檢索數據,但不是在recordsCtrl函數init()中搜索。
我來源:
查看:jalse.html:
<div class="content-panel" ng-init="init()">
<div class="content-panel-title">
<h2> Date : {{jalse.contentDate}}</h2>
</div>
<div>
<h4>{{jalse.contentAbstract}}</h4>
<div>
{{jalse.contentText}}
</div>
</div>
</div>
控制器: recordsCtrl.js:
(function() {
'use strict';
var recordsCtrl = function($scope, $routeParams , jasleFactory) {
var jalseId = $routeParams.jalseId;
jasleFactory.getJalse().success(function(data, status, headers, config) {
$scope.records = data;
}).
error(function(error, status, headers, config) {
console.log("error occured " + error)
});
$scope.jalse = null;
function init() {
for (var i = 0, len = $scope.records.length; i < len; i++) {
if ($scope.records[i].contentID == parseInt(jalseId)) {
$scope.jalse = $scope.records[i];
break;
}
}
}
};
recordsCtrl.$inject = ['$scope','$routeParams', 'jasleFactory'];
angular.module('myApp').controller('recordsCtrl', recordsCtrl);
}());
廠: jalseFactory.js:
(function() {
var jasleFactory = function ($http) {
var factory = {};
factory.getJalse = function() {
return $http({method: 'GET', url: 'includes/records.php'});
};
return factory;
};
jasleFactory.$inject = ['$http'];
angular.module('myApp').factory('jasleFactory', jasleFactory);
}());
個
文件app.js:
(function() {
var app = angular.module('myApp', ['ngRoute']);
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
controller: 'contentsCtrl',
templateUrl: 'views/contents.php'
})
.when('/jalse/:jalseId', {
controller: 'recordsCtrl',
templateUrl: 'views/jalse.php'
})
.otherwise({redirectTo: '/'});
});
}());
感謝。我在上面添加了代碼但是沒有解決 – Mehdi
控制檯中的任何錯誤?在控制檯的網絡選項卡上提出要求並獲得成功? – charlietfl
沒有錯誤 - 我打印記錄在視圖中但不是搜索 – Mehdi