当前位置:  开发笔记 > 编程语言 > 正文

使用AngularJS奇怪链接到/ undefined

如何解决《使用AngularJS奇怪链接到/undefined》经验,为你挑选了0个好方法。

使用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
        
        
        
        
        
        
        
        
    
    

    
    

    
依然-狠幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有