AngularJS Uncaught TypeError:无法读取undefined的属性'replace'

时间:2015-07-29 07:39:51

标签: angularjs

当我的角度应用程序启动时,它会立即在Angular.js的第4414行引发错误(v1.4.3)。在初始化(引导)阶段,似乎无法加载服务。当我按F5继续时,_Layout.chtml被加载。但是,ui-view不是从Index.cshtml加载的。这表示Home控制器未执行,因此未填充$ scope。它应该处于“Home”ui-state状态,但它没有使用url“http://localhost/myApp/”进入该状态。谢谢你的帮助。

从Chrome控制台

trimEmptyHash@ angular.js: 11267
$get@ angular.js: 12096
invoke@ angular.js: 4450(anonymous
  function)@ angular.js: 4268
getService@ angular.js: 4409
invoke@ angular.js: 4441(anonymous
  function)@ angular.js: 4268
getService@ angular.js: 4409
invoke@ angular.js: 4441(anonymous
  function)@ angular.js: 4268
getService@ angular.js: 4409
invoke@ angular.js: 4441(anonymous
  function)@ angular.js: 4272
forEach@ angular.js: 336
createInjector@ angular.js: 4272
doBootstrap@ angular.js: 1630
bootstrap@ angular.js: 1651
angularInit@ angular.js: 1545(anonymous
  function)@ angular.js: 28359
jQuery.Callbacks.fire@ jquery - 2.1.4.js: 3099
jQuery.Callbacks.self.fireWith@ jquery - 2.1.4.js: 3211
jQuery.extend.ready@ jquery - 2.1.4.js: 3417
completed@ jquery - 2.1.4.js: 3433

这是callstack和断点 enter image description here

当点击_layout.cshtml呈现的菜单中的任何下拉列表时,我得到“无法获取未定义或空引用的属性'拆分”。它认为URL是“未定义的”。

enter image description here

这是我的App.js

/// <reference path="E:\Work\App\AngularSPA\AngularSPA\Views/TasksEndedTable .cshtml" />

'use strict';

angular.module('App', ['ui.router', 'App.controllers', 'App.filters', 'App.services', 'ui.bootstrap'])

// Gets executed during the provider registrations and configuration phase. Only providers and constants can be
// injected here. This is to prevent accidental instantiation of services before they have been fully configured.
.config(['$stateProvider', '$locationProvider',
  function($stateProvider, $locationProvider) {

    // UI States, URL Routing & Mapping. For more info see: https://github.com/angular-ui/ui-router
    // ------------------------------------------------------------------------------------------------------------

    $stateProvider
      .state('home', {
        url: '/',
        templateUrl: 'views/index',
        controller: 'HomeCtrl'
      })
      .state('about', {
        url: '/about',
        templateUrl: 'views/about',
        controller: 'AboutCtrl'
      })
      .state('contacts', {
        url: '/contacts',
        templateUrl: 'views/contacts',
        controller: 'ContactsCtrl'
      })
      .state('login', {
        url: '/login',
        templateUrl: 'views/login',
        controller: 'LoginCtrl'
      })
      .state('preferences', {
        url: '/preferences',
        templateUrl: 'views/preferences',
        controller: 'PreferenceCtrl'
      })
      .state('uiGrid', {
        url: '/uigrid',
        templateUrl: 'views/uigrid.cshtml',
        controller: 'UIGridCtrl'
      })
      // Task routes -----------------------------
      .state('taskList', {
        url: '/:listType',
        templateUrl: 'views/tasksList.cshtml',
        controller: 'TaskCtrl'
      })
      .state('taskSearchForm', {
        url: '/taskSearch/:formType',
        templateUrl: 'views/taskSearch.cshtml',
        controller: 'TaskSearchCtrl'
      })
      .state('taskEdit', {
        url: '/taskEdit/:id',
        templateUrl: 'views/taskEdit.cshtml',
        controller: 'TaskEditCtrl'
      })
      .state('taskAdd', {
        url: '/taskAdd',
        templateUrl: 'views/taskAdd.cshtml',
        controller: 'TaskAddCtrl'
      })

    // User routes -----------------------------
    .state('users', {
        url: '/users',
        tempalteUrl: 'views/users',
        controller: 'UserCtrl',
      })
      .state('addUser', {
        url: '/addUser',
        tempalteUrl: 'views/userAdd',
        controller: 'UserAddCtrl',
      })

    // Otherwise routes -----------------------------
    .state('otherwise', {
      url: '*path',
      templateUrl: 'views/404',
      controller: 'Error404Ctrl'
    });

    $locationProvider.html5Mode(true);

  }
])

.run(['$templateCache', '$rootScope', '$state', '$stateParams',
  function($templateCache, $rootScope, $state, $stateParams) {

    // <ui-view> contains a pre-rendered template for the current view
    // caching it will prevent a round-trip to a server at the first page load
    var view = angular.element('#ui-view');
    $templateCache.put(view.data('tmpl-url'), view.html());

    // Allows to retrieve UI Router state information from inside templates
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;

    $rootScope.$on('$stateChangeSuccess',
      function(event, toState) {

        // Sets the layout name, which can be used to display different layouts (header, footer etc.)
        // based on which page the user is located
        $rootScope.layout = toState.layout;
      });
  }
]);

1 个答案:

答案 0 :(得分:6)

确保您的IIS应用程序名称全部为小写。 (可能必须删除它并重新创建它,如果没有)

这样它就会与您查看源时看到的内容相匹配,以查看应用的基本标记。

<base href="http://case/sensitivepath" />

它匹配IIS中的内容(如.. \ wwwroot \ sensitivepath)

最好还检查所有路线案例问题。