Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

AngularJS with dependency injection

Options
  • 13-08-2014 5:41pm
    #1
    Registered Users Posts: 4,010 ✭✭✭


    I have a problem with a project I am working on. It's an MVC .NET web app with AngularJS. I am a complete newbie to AngularJS but I know some MVC and .NET.
    Basically I am having a problem with the part where AngularJS injects the provider into the factory method (see below).
    HotelApp.factory('Hotel', function ($resource) {...
    

    'Hotel' is a C# class with properties and functions etc. It goes into the line above and after that I have set breakpoints where I am using 'Hotel' after that. It hits the breakpoints but 'Hotel' always comes up as "undefined".
    I don't want to post the code as what I really want is to see an example of dependency injection in AngularJS but using an instantiable class .
    I have seen loads of examples of AngularJS dependency injection online but none using a class, it always functions or a service.
    If I could see a working example I think I could fix my project. It must be a .NET MVC project and it is a web app.


Comments

  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    You posted more code on StackOverflow! :D


  • Registered Users Posts: 4,010 ✭✭✭lukin


    You posted more code on StackOverflow! :D

    That's mad that you found that, your googling skills must be top notch:)
    I actually found an example of what I am looking for:https://github.com/jph00/AngularTutorial/tree/master/AngularTutorial (I mentioned it on my post on StackOverflow) but even though I coded mine like the one on github, the class that is injected in mine is coming up as "undefined" but the one on github is being defined. I think the github example is a little too complicated, I just want something simple that I could compare mine with.
    I didn't post code because it could be any one of a number of things.


  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    My point was you should post more code. I've posted the full answer somewhere on Google. Let's see how your skills stack up! :D

    http://stackoverflow.com/questions/25288049/angularjs-dependency-injection-of-an-instantiable-class

    Short answer is, I don't think your use of $resource is doing what you think it's doing...


  • Registered Users Posts: 4,010 ✭✭✭lukin


    I got it sorted anyway (well I actually got somebody to sort it for me). It wasn't actually anything to do with the line I posted
    HotelApp.factory('Hotel', function ($resource) {...
    

    Issue was with the controller function declared in the .js file that does all the angularjs stuff. The object class I want to inject had to be passed into this function

    It was
    function hotelController($scope, $http) {
    
    but when changed to
    function hotelController($scope, Hotel) {
    

    the Hotel variable then had a value and could be used anywhere else within the js file. There is a Controller class called HotelController.cs in the project.
    Probably meat and drink to AngularJS experts reading this but I said I would follow it up in case anyone does a search for something like I wanted to know.


Advertisement