AngularJS 未知类型错误:无法读取 null 的属性“1”

AngularJS Unknown TypeError: Cannot Read Property '1' Of Null

本文关键字:null 属性 读取 类型 未知 错误 AngularJS      更新时间:2023-09-26

下面是我遇到问题的代码:

//Main Codes for the Diary App
(function () {
//Create a new diary app
var diary = angular.module('diary', ['ngRoute']);
//Define a Router for the App
diary.config(function ($routeProvider) {
        $routeProvider.

            when('/', {
                templateUrl: 'partials/wall.php',
                controller: 'DiaryWallController'
            }).

            when('/images', {
                templateUrl: 'partials/images.html',
                controller: 'DiaryImagesController'
            }).

            when('/audio', {
                templateUrl: 'partials/audio.html',
                controller: 'DiaryAudioController'
            }).

            otherwise({
                redirectTo: '/'
            });
});
//Define Controllers
diary.controller('DiaryWallController', function($scope) {
    $scope.message = "Lets do this!! Wall of this site";
});
diary.controller('DiaryImagesController', function($scope) {
    $scope.message = "Lets do this!! Images of this site";
});
diary.controller('DiaryAudioController', function($scope) {
    $scope.message = "Lets do this!! Audio of this site";
});})();

我收到此未知类型错误:无法读取 NuLL 的属性"1"。它成功获取路由中指定的文件,但不显示在站点中。我已经检查了控制台:

XHR finished loading: GET "http://localhost/mobile%20diary/app/partials/images.html". 

但是加载的页面的内容不会显示在网站中。我签入资源并在那里加载图像.html文件,我可以看到文件预览,但它没有显示在站点中。

下面是 HTML Shell 文件:

<!DOCTYPE html>
<html ng-app="diary">
<head lang="en" ng-controller="">
<meta charset="UTF-8">
<link rel="stylesheet" href="css/bootstrap.css" type="text/css">
<link rel="stylesheet" href="css/style.css" type="text/css">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title></title>
</head>
<body>

<header>
<nav class="menu-nav">
    <ul>
        <li class="menu-buttons"><a href="#" id="selected"><i class="fa fa-newspaper-o"></i></a></li>
        <li class="menu-buttons"><a href="#images"><i class="fa fa-file-text-o"></i></a></li>
        <li class="menu-buttons"><a href="#audio"><i class="fa fa-image"></i></a></li>
        <li class="menu-buttons"><a href=""><i class="fa fa-microphone"></i></a></li>
        <li class="menu-buttons"><a href="" id="selected"><i class="fa fa-navicon"></i></a></li>
    </ul>
</nav>
</header>
<div ng-view>
</div>

<script src="js/angular.js"></script>
<script src="js/angular-route.min.js"></script>
<script src="js/app.js"></script>
</body>
</html>

我也有同样的错误,类似于@ChanX的评论,我在ng控制器属性中遇到了错误。 就我而言,我不小心将一个 css 类的名称错误地添加到 ng-controller 属性中,我打算将其放入类属性中。

我的假设是,每当解析 ng-controller 属性时出现问题时,您都可能会看到此错误。 无论如何,我当然很高兴我在 SO 上找到了这个,因为它是一个真正的令人头疼的问题。 如果错误消息信息量更大,那就太好了。

这似乎是因为您使用的是本地主机网址。可能适用于调试或开发环境。

content_script.js上有一个函数如下:

function scanLinks() {
  var current_domain = document.location.href.match(/^https?:'/'/(?:www'.)?([^'.]+'.[^'/]+)/i);
  $("a[href][muse_scanned!=true]").each(function(i) {
    this.setAttribute("muse_scanned", true);
    var href = this.href;
    var domain = href.match(/^http:'/'/(?:(?:www'.)?(?:[^'.]+'.(notlong'.com|qlnk'.net|ni'.to|lu'.to|zzang'.kr)|([^'.]+'.[^'/]+)))/i);
    if (domain) {
      domain = domain[1] || domain[2] || false;
    }
    if ((domain != current_domain[1]) && (expandSvcList.indexOf(domain) > -1)) {
      this.title = "Expanding URL...";
      expandLink(this);
    }
  });
}

重要的部分是:

var current_domain = document.location.href.match(/^https?:'/'/(?:www'.)?([^'.]+'.[^'/]+)/i);

这将检查您当前的链接,在您的情况下为"http://localhost/..."对于有效的网址,例如"http://www.localhost/"。因为不是这种情况,所以它在访问current_domain[1]时会导致错误,因为current_domain为 null。