Angular not run指令

时间:2015-03-04 05:53:23

标签: angularjs

为什么当我运行我的Angular应用程序时,我没有得到指令运行?

示例代码:

var app = angular.module("app",['mgcrea.ngStrap','layout.menu']);

app.controller('MainController', ['$scope','$timeout', function($scope,$timeout){
    $timeout(function(){console.log("ready")});
}]);

var menu = angular.module('layout.menu', [])

.controller('MenuController', ['$scope', function($scope){
    console.log("controller");
}])

.directive('menuDir', ['$window', function($window){
    console.log("directive");
    return function (scope, element) {
         console.log("return directive");
    };
}]);

HTML:

<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" type="text/css" href="assets/libs/bootstrap/dist/css/bootstrap.min.css">
    <script src="assets/libs/jquery/dist/jquery.min.js"></script>
    <script type="text/javascript" src="assets/libs/angular/angular.js"></script>
    <script type="text/javascript" src="assets/libs/angular-strap/dist/angular-strap.js"></script>
    <script src="assets/libs/angular-strap/dist/angular-strap.tpl.min.js"></script>
    <script type="text/javascript" src="app.js"></script>
</head>
<body ng-controller="MainController">
    <div class="list-group mainmenu" id="mainmenu" ng-controller="MenuController"></div>
</body>
</html>

当我运行应用程序时,我得到controller输出,但没有来自指令的输出。为什么?我该如何解决?

1 个答案:

答案 0 :(得分:1)

angularJs中的指令/服务/工厂被懒惰地实例化。

只有在我们使用它们时才会处理它们。

在标记中使用menuDir指令,然后您将看到在指令中写入的控制台语句。