无法在AngularJS中将指令注入为依赖项

时间:2015-12-20 07:56:21

标签: javascript angularjs dependency-injection

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

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;
})

1 个答案:

答案 0 :(得分:3)

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

angular.module('firstdirective', [])

否则您正在尝试检索模块。