0

So I've been looking at this for way too long. Really hope someone could help me out :) I'm just trying to create a module that creates a directive and controller for my site header in AngularJS. I don't get any error and the log in my code won't show up. This is the code related to the header module:

header/header.js

 'use strict';
angular.module('myApp.header', [])

.directive("headerBar", [function(){
  return {
    restric: "E",
    templateUrl: "header/header.html",
    controller: 'HeaderCtrl'
  };
}])

.controller('HeaderCtrl', ['$log', function($log) {
  $log.log('test header controller');
}]);

app.js

angular.module('myApp', [
  'ngRoute',
  'myApp.header'
]).
config(['$routeProvider', function($routeProvider) {
  $routeProvider.otherwise({redirectTo: '/'});
}]);

index.html

<script src="header/header.js"></script>


<header-bar></header-bar>
2
  • Have you initialized the app ? Commented Apr 17, 2015 at 10:53
  • Why would I add an object-related directive to my main module? And yes, I initialized the app, the routing modules work perfectly. Commented Apr 17, 2015 at 10:56

3 Answers 3

1

I see a typo on your directive definition:

  • Should be restrict: "E", you're currently missing the 't'
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, totally overlooked that. Thanks for noticing! For the other possible reasons you had mentioned before (and the other answers): In the original post I said that I only posted the code related to the issue, so all my includes and so on were already there. But again, thanks for the quick help. It works!
I noticed that after re-reading, so I edited my post to remove all those other points. Glad to help!
1

You need to call the app.js and angular.js reference scripts in your index page

Some think like below

   //Jquery Scripts
   //Angular.js library scripts 
  <script src="header/header.js"></script>
  **<script src="app.js"></script>**// Please refer here your app.js script



<header-bar></header-bar>

Comments

1

This works for me.Please check that you are including all files.

<html>
<head>
  <title>test</title>

  <link rel="stylesheet" href="styles.css">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
      <script src="header/header.js"></script> 
        <script src="app.js"></script> 
</head>

<body ng-app="myApp.header" ng-controller="HeaderCtrl">
<header-bar></header-bar>

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.