gpt4 book ai didi

javascript - 无法将指令作为 AngularJS 中的依赖项注入(inject)

转载 作者:行者123 更新时间:2023-11-28 18:51:49 25 4
gpt4 key购买 nike

我正在学习创建指令并通过依赖项添加使其可用,但不知何故它不起作用,让我知道我做错了什么。

Plnkr - http://plnkr.co/edit/Mk4h7XvSN2YA26JZZ9Ua?p=preview

index.html

<!DOCTYPE html>
<html lang="en" ng-app="myApp">

<head>
<script src="https://code.angularjs.org/1.4.7/angular.js"></script>
<script type="text/javascript" src="firstDirective.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>

<body ng-controller="mainCtrl as vm">
<div class="container">
<simple-directive></simple-directive>
</div>
</body>
</html>

firstDirective.js

angular
.module('firstdirective')
.directive('simpleDirective', function(){
return {
scope: true, // inherit child scope from parent
restrict: 'E',
replace: true,
template: '<h2>My first directive from dependency</h2>'
};
});

script.js

var myApp = angular.module('myApp', ['firstdirective']);
myApp.controller('mainCtrl', function(){
var vm = this;
})

最佳答案

要创建新模块,您需要传递依赖项数组作为第二个参数:

angular.module('firstdirective', [])

否则您将尝试检索该模块。

关于javascript - 无法将指令作为 AngularJS 中的依赖项注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34379001/

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