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



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

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', [])

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

最新更新