gpt4 book ai didi

javascript - 在 DOM 上执行多个指令

转载 作者:行者123 更新时间:2023-11-30 16:50:55 27 4
gpt4 key购买 nike

在AngularJs1.3的一个模块中定义了2条指令

我发现只有 1 个指令被执行,虽然当另一个指令被注释掉时这 2 个指令单独工作。
执行控制台没有异常。

指令

angular.module('studentDetailApp.directives',[]).directive('studentDirective', function () {
return {
template:'...',
link: function ($scope, element, attrs) { console.log('student directive');}
}
})
.directive('basicDirective', function () {
return {
restrict:'E',
template:'custom directive: {{textToInsert}}',
link:function ($scope, element, attrs) { console.log('Printing out custom template');}
}
});

index.html

<html lang="en" ng-app="studentDetailApp">
<head>
....
</head>
<body>
<div ng-controller="StudentController">
<basic-directive/>
<div student-directive></div>
</div>
</body>
</html>

我发现第二个指令有效 - 而第一个指令被忽略了。
错误是什么?

最佳答案

您需要关闭 basic-directive元素恰如其分 <basic-directive></basic-directive> ,元素标签本质上不是自闭的,期待img , br , hrList of self closing tags

标记

<div ng-controller="StudentController"> 
<basic-directive></basic-directive>
<div student-directive></div>
</div>

关于javascript - 在 DOM 上执行多个指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30544551/

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