Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

AngularJS with dependency injection

  • 13-08-2014 05:41PM
    #1
    Registered Users, Registered Users 2 Posts: 4,327 ✭✭✭


    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, Registered Users 2 Posts: 2,062 ✭✭✭Colonel Panic


    You posted more code on StackOverflow! :D


  • Registered Users, Registered Users 2 Posts: 4,327 ✭✭✭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, Registered Users 2 Posts: 2,062 ✭✭✭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, Registered Users 2 Posts: 4,327 ✭✭✭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