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
Hi all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

AngularJS with dependency injection

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


    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,029 ✭✭✭Colonel Panic


    You posted more code on StackOverflow! :D


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