2
我剛開始學習AngularJS,並建立一個網站來發展我的技能。AngularJS 1延遲路線,直到模型被加載
我正在嘗試延遲角度從路由到新頁面,直到JSON數據加載。
我無法找到堆棧溢出的答案,它使用角度工廠來加載JSON。
我的檔案如下。
services.js
app.factory('contactFormData', ['$http', function($http, $q) {
return $http.get('json/eager-contactform.json')
.success(function(data) {
return data;
})
.error(function(err) {
return err;
})
}])
ContactController.js
app.controller('ContactController', ['$scope', 'contactFormData', '$http', function($scope, contactFormData, $http) {
contactFormData.success(function(data) {
$scope.contactFormData = data;
})
$scope.success = false;
$scope.error = false;
$scope.sendMessage = function(value) {
$http({
method: 'POST',
url: 'php/eagerContact.php',
data: value,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})
.success(function(data) {
if (data.success) {
$scope.success = true;
} else {
$scope.error = true;
}
});
}
}]);
app.js
var app = angular
.module("eager", ['ngSanitize', 'ngRoute'])
.config(function ($routeProvider) {
$routeProvider
.when('/contact', {
controller: 'ContactController',
templateUrl: 'views/contact.html',
})
.otherwise({
redirectTo: '/'
});
});
誰能幫助我?