TypeError:无法读取属性' setAttribute' null

时间:2018-03-23 11:34:54

标签: javascript jquery angularjs angular-material

我正在构建一个角度项目,其中集成了angular-datatables(dataTable.js的角度指令)。当我将<md-menu></md-menu>角度材料注入到DOM中的angular-datatable时,我在浏览器控制台中显示附加的图像错误。请查看生成的错误,如下所示 enter image description here

代码工作正常,但我想摆脱浏览器控制台中的恼人错误。仅在DOM第一次加载或重新加载时抛出错误。

最初,我认为错误被抛出是因为jquery在DOM完全加载之前加载,直到我在关闭体</body>标记之前将javascript文件重新定位在DOM的基础上。

这对我的项目不利,因为随着我的进一步构建,东西可能会变得混乱。我在研究过程中看到了类似的在线错误,但所提供的解决方案都没有解决我的问题。

请帮帮我。我真的不知道自己做错了什么或者如何解决这个问题。请参阅plunker上的DEMO PROJECT。谢谢。

4 个答案:

答案 0 :(得分:2)

angular-material.js第33024行下面的两行代码负责此js错误。

triggerElement = $element[0].querySelector('[ng-click],[ng-mouseenter]');
triggerElement.setAttribute('aria-expanded', 'false');

当md-menu中没有包含ng-click或ng-mouseenter属性的元素时,triggerElement为null。 因此,从md-menu元素移动ng-click到md-icon元素。在json下面使用。

    [
    {
        "id":1,"firstname":"Frederick","lastname":"Eze","phone":"2348094959121","manage":
        "<md-menu><md-icon ng-click=\"$mdMenu.open($event)\" md-svg-icon=\"dots-vertical\"></md-icon><md-menu-content><md-menu-item ng-repeat=\"item in ctrl.actions\"><md-button>{{item.action}}</md-button></md-menu-item></md-menu-content></md-menu>"
    },
    {
        "id":2,"firstname":"John","lastname":"Doe","phone":"2348094449128","manage":"<md-menu><md-icon ng-click=\"$mdMenu.open($event)\" md-svg-icon=\"dots-vertical\"></md-icon><md-menu-content><md-menu-item ng-repeat=\"item in ctrl.actions\"><md-button>{{item.action}}</md-button></md-menu-item></md-menu-content></md-menu>"
    },
    {
        "id":3,"firstname":"Mary","lastname":"Laz","phone":"2348094956690","manage":"<md-menu><md-icon ng-click=\"$mdMenu.open($event)\" md-svg-icon=\"dots-vertical\"></md-icon><md-menu-content><md-menu-item ng-repeat=\"item in ctrl.actions\"><md-button>{{item.action}}</md-button></md-menu-item></md-menu-content></md-menu>"
    },
    {
        "id":4,"firstname":"Eunice","lastname":"Akor","phone":"2348087959125","manage":"<md-menu><md-icon ng-click=\"$mdMenu.open($event)\" md-svg-icon=\"dots-vertical\"></md-icon><md-menu-content><md-menu-item ng-repeat=\"item in ctrl.actions\"><md-button>{{item.action}}</md-button></md-menu-item></md-menu-content></md-menu>"
    },
    {
        "id":5,"firstname":"Kenneth","lastname":"Dues","phone":"2348094959178","manage":"<md-menu><md-icon ng-click=\"$mdMenu.open($event)\" md-svg-icon=\"dots-vertical\"></md-icon><md-menu-content><md-menu-item ng-repeat=\"item in ctrl.actions\"><md-button>{{item.action}}</md-button></md-menu-item></md-menu-content></md-menu>"
    }
]

答案 1 :(得分:1)

试试这个

[
    {
        "id":1,"firstname":"Frederick","lastname":"Eze","phone":"2348094959121","manage":"<md-menu ><md-button class=\"md-icon-button\" ng-click=\"$mdMenu.open($event)\"><md-icon md-menu-origin=\"\" md-svg-icon=\"dots-vertical\"></md-icon></md-button><md-menu-content><md-menu-item ng-repeat=\"item in ctrl.actions\"><md-button>{{item.action}}</md-button></md-menu-item></md-menu-content></md-menu>"
    },
    {
        "id":2,"firstname":"John","lastname":"Doe","phone":"2348094449128","manage":"<md-menu ><md-button class=\"md-icon-button\" ng-click=\"$mdMenu.open($event)\"><md-icon md-menu-origin=\"\" md-svg-icon=\"dots-vertical\"></md-icon></md-button><md-menu-content><md-menu-item ng-repeat=\"item in ctrl.actions\"><md-button>{{item.action}}</md-button></md-menu-item></md-menu-content></md-menu>"
    },
    {
        "id":3,"firstname":"Mary","lastname":"Laz","phone":"2348094956690","manage":"<md-menu ><md-button class=\"md-icon-button\" ng-click=\"$mdMenu.open($event)\"><md-icon md-menu-origin=\"\" md-svg-icon=\"dots-vertical\"></md-icon></md-button><md-menu-content><md-menu-item ng-repeat=\"item in ctrl.actions\"><md-button>{{item.action}}</md-button></md-menu-item></md-menu-content></md-menu>"
    },
    {
        "id":4,"firstname":"Eunice","lastname":"Akor","phone":"2348087959125","manage":"<md-menu ><md-button class=\"md-icon-button\" ng-click=\"$mdMenu.open($event)\"><md-icon md-menu-origin=\"\" md-svg-icon=\"dots-vertical\"></md-icon></md-button><md-menu-content><md-menu-item ng-repeat=\"item in ctrl.actions\"><md-button>{{item.action}}</md-button></md-menu-item></md-menu-content></md-menu>"
    },
    {
        "id":5,"firstname":"Kenneth","lastname":"Dues","phone":"2348094959178","manage":"<md-menu ><md-button class=\"md-icon-button\" ng-click=\"$mdMenu.open($event)\"><md-icon md-menu-origin=\"\" md-svg-icon=\"dots-vertical\"></md-icon></md-button><md-menu-content><md-menu-item ng-repeat=\"item in ctrl.actions\"><md-button>{{item.action}}</md-button></md-menu-item></md-menu-content></md-menu>"
    }
]

将点击功能移至

<md-button class=\"md-icon-button\" ng-click=\"$mdMenu.open($event)\"><md-icon md-menu-origin=\"\" md-svg-icon=\"dots-vertical\"></md-icon></md-button>

答案 2 :(得分:0)

只需使用以下代码替换&#34; services.js&#34; 文件中的第42行

if(iElement.assignedSlot){
$compile(iElement.contents())(scope);
}

我希望这对你有用。

答案 3 :(得分:-3)

尝试将值更改为null以外的其他值。