gpt4 book ai didi

javascript - Angularjs:TypeError:无法调用 null 的方法 'insertBefore'

转载 作者:可可西里 更新时间:2023-11-01 01:37:40 26 4
gpt4 key购买 nike

请在这里找到 fiddle http://jsfiddle.net/UxYLa/6/

这是我正在尝试做的事情的简化形式。有两个指令,一个是嵌套指令,subDirective,它根据选择(随机)动态创建 html 表单。如果你重复点击按钮,它会抛出以下错误

TypeError: Cannot call method 'insertBefore' of null
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js:138:283
at q (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js:7:332)
at q.after (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js:138:258)
at Object.O.(anonymous function) [as after] (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js:139:414)
at Object.enter (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js:141:226)
at Object.move (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js:141:360)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js:185:282
at Object.fn (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js:99:371)

我找到了关于这个的引用 https://groups.google.com/forum/#!msg/angular/dNra_7P2hwU/09-UBn1XxyUJ https://github.com/angular/angular.js/issues/2151但我使用的是最新的稳定版本。

这个错误的原因是什么。

完整代码如下:

脚本:

var myApp = angular.module('myApp', [])
.controller('TestController', ['$scope', function ($scope) {
$scope.data = [{ type: "a", values: [{name:"aa"}, {name: "bb"}]},
{ type: "b", values: [{name:"aa"}, {name: "bb"}]}]
}]);
myApp.directive('directive', function () {
return {
scope: {
data: "=",
},
restrict: 'E',
controller: function ($scope, $element, $attrs) {
$scope.random = function(){
$scope.model = $scope.data[Math.floor(Math.random()*$scope.data.length)];
console.log($scope.model)
};
},
template: '<div><button ng-click="random()">Random</button><sub-directive data="model"></sub-directive></div>'
};
});


myApp.directive('subDirective', function ($templateCache, $compile) {
return {
scope: {
data: "=",
},
restrict: 'E',
link: function (scope, element, attrs) {
scope.$watch('data', function (newVal, oldVal) {
if (newVal) {
element.html($templateCache.get(newVal.type + '.html'));
$compile(element.contents())(scope);
}
});
}
};
});

html

  <script type="text/ng-template" id="a.html">
a.html
</script>


<script type="text/ng-template" id="b.html">
<div ng-repeat="itm in data">
<span ng-if='"string" == itm.type'>
<input name='{{itm.Name}}'id='{{itm.Name}}' ng-model='model[itm.Name]' type='text'></input>
</span>
</div>
</script>

<div ng-controller="TestController">
<directive data="data"></directive>
</div>

最佳答案

我有同样的错误信息。我可以通过在我的 html 脚本标记中在 Angular 之前引用 JQuery 来解决这个问题。

关于javascript - Angularjs:TypeError:无法调用 null 的方法 'insertBefore',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21419368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com