Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with angular when trying to use with latest version(0.9.3) #213

Closed
ghost opened this issue Apr 22, 2016 · 11 comments
Closed

Error with angular when trying to use with latest version(0.9.3) #213

ghost opened this issue Apr 22, 2016 · 11 comments

Comments

@ghost
Copy link

ghost commented Apr 22, 2016

AngularJS throws an error when latest files are included:
Error: $injector:undef
Undefined Value
Provider 'HeadroomService' must return a value from $get factory method.

@WickyNilliams
Copy link
Owner

Could you create a fiddle (or pen or bin or whatever) to demonstrate the issue?

@moosi
Copy link

moosi commented Apr 28, 2016

+1

I simply added headroom above the tab bar: https://plnkr.co/edit/b6panE?p=info

@moosi
Copy link

moosi commented May 19, 2016

Are there any updates on this issue?

@sandor11
Copy link

Have no idea if this is helpful, or not but I just hacked this up in the last 15 mins. I had problems after minification, but was getting the same error you are seeing. Don't know if this will work for you.

/*!
 * headroom.js v0.9.3 - Give your page some headroom. Hide your header until you need it
 * Copyright (c) 2016 Nick Williams - http://wicky.nillia.ms/headroom.js
 * License: MIT
 */

(function (angular) {

  if(!angular) {
    return;
  }

  function hFactory() {
    return {
        headroom: function() {return window.Headroom;}
    };
  }

  function headroom(HeadroomFactory) {
    return {
      scope: {
        tolerance: '=',
        offset: '=',
        classes: '=',
        scroller: '@'
      },
      link: function ($scope, $element) {
        var head = HeadroomFactory.headroom();
        var options = {};
        var opts = head.options;
        for (var prop in opts) {
          options[prop] = $scope[prop] || opts[prop];
        }
        if ($scope.scroller) {
          options.scroller = document.querySelector($scope.scroller);
        }
        var headroom = new head($element[0], options).init();
        $scope.$on('$destroy', function(){
          headroom.destroy();
        });
      }
    };
  }

  headroom.$inject = ['HeadroomFactory'];

  angular
    .module('headroom', [])
    .directive('headroom', headroom)
    .factory('HeadroomFactory', hFactory);

})(window.angular);

@moosi
Copy link

moosi commented Jul 6, 2016

Any updates regarding this issue?

@jenyckee
Copy link

jenyckee commented Jul 7, 2016

+1

@shibanovp
Copy link

This happens because window.Headroom is undefined. Make sure you've loaded headroom.js

@atsoy atsoy mentioned this issue Jan 16, 2017
@rwoody
Copy link

rwoody commented Feb 20, 2017

I'm using CommonJS modules (Webpack 2) and I had this same issue.

When CommonJS/AMD is used, the window.Headroom does not get populated, so the Angular module breaks. To solve this I used the imports-loader for Webpack to force this to be window and set define and exports to false to prevent the module style stuff from executing:

In webpack.config.js:

rules: [
  {
    test: require.resolve('headroom.js'),
    use: 'imports-loader?this=>window,define=>false,exports=>false'
  }
]

In app.js:

import 'headroom.js'
import 'headroom.js/dist/angular.headroom'

// ...

angular.module('MyApp', ['headroom'])

@mcblum
Copy link

mcblum commented Jun 27, 2017

@rwoody any idea how this would look in Webpack 1? I seem to be having the same issue but cannot figure it out!

@mcblum
Copy link

mcblum commented Jun 27, 2017

update: fixed it for anyone who may need this code:

 // expose Headroom
 new webpack.ProvidePlugin({
    'window.Headroom': 'headroom.js/dist/headroom.js'
})

@WickyNilliams
Copy link
Owner

Closing in favour of #333, which will look at all angular issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants