使用Codeigniter和AngularJS加载不同的模板时遇到奇怪的问题.当我点击其他链接时,模板会在某些/未定义的链接上重定向.让我告诉你我的代码.
这是我的app.js.
var app = angular.module('app', ['ngRoute']); app.config(function($routeProvider){ $routeProvider. when('/', {controller:'homeCtrl', templateUrl:'app/templates/home.html'}). when('/home', {controller:'homeCtrl', templateUrl:'app/templates/home.html'}). when('/contact', {controller:'contactCtrl', templateUrl:'app/templates/contact.html'}). otherwise({ redirectTo: '/home'}); });
controllers.js
var app = angular.module('app'); var controllers = {}; controllers.headerCtrl = function($scope, categoriesFactory){ //get available categories categoriesFactory.getCategoriesList().success(function(data){ $scope.categories = data; }).error(function(e){ console.log(e); }); } controllers.homeCtrl = function($scope, productsFactory){ productsFactory.getlatestProductsList(16).success(function(data){ $scope.products = data; }).error(function(e){ console.log(e); }); } controllers.contactCtrl = function($scope, $http, $location){ //Send message // creating a blank object to hold our form information. //$scope will allow this to pass between controller and view $scope.formData = {}; // submission message doesn't show when page loads $scope.submission = false; // Updated code thanks to Yotam var param = function(data) { var returnString = ''; for (d in data){ if (data.hasOwnProperty(d)) returnString += d + '=' + data[d] + '&'; } // Remove last ampersand and return return returnString.slice( 0, returnString.length - 1 ); }; $scope.submitForm = function(){ $scope.submissionMessage = ''; $http({ method : 'POST', url : $location.protocol() + '://' + $location.host() + '/server/contact/send_message', data : param($scope.formData), // pass in data as strings // set the headers so angular passing info as form data (not request payload) headers : { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function(data){ if(!data.success){ var name = document.getElementById('Name').value; var email = document.getElementById('email').value; var subject = document.getElementById('subject').value; var message = document.getElementById('message').value; if(name.length == '' && email.length == '' && subject.length == '' && message.length == ''){ $scope.submissionMessage = data.messageError; } // if not successful, bind errors to error variables $scope.errorName = data.errors.name; $scope.errorEmail = data.errors.email; $scope.errorSubject = data.errors.subject; $scope.errorTextarea = data.errors.message; $scope.submission = true; //shows the error message }else{ // if successful, bind success message to message $scope.submissionMessage = data.messageSuccess; $scope.formData = {}; // form fields are emptied with this line $scope.submission = true; //shows the success message $scope.errorName = ''; $scope.errorEmail = ''; $scope.errorSubject = ''; $scope.errorTextarea = ''; } }); }; } app.controller(controllers);
Factorys.js
var app = angular.module('app'); //Factory for categories app.factory('categoriesFactory', ['$http', '$location', function($http, $location){ var factory = {}; factory.getCategoriesList = function(){ return $http.get($location.protocol() + '://' + $location.host() + '/server/api/categories'); } return factory; }]); //Factory for products app.factory('productsFactory', ['$http', '$location', function($http, $location){ var factory = {}; /* factory.getProductsList = function(){ return $http.get($location.protocol() + '://' + $location.host() + '/server/api/products'); } */ factory.getlatestProductsList = function($n){ return $http.get($location.protocol() + '://' + $location.host() + '/server/api/products/latest/'+$n); } return factory; }]);
我的HTML index.html
Trade inside europe