Code Monkey home page Code Monkey logo

Comments (3)

goodbomb avatar goodbomb commented on June 9, 2024

Actually, if you look at the index.js file that's in a module folder, that is where the controller for the module is defined. The controller defined in the Directive is simply specifying the controller used for that Directive (which happens to be the same as the module controller).

That said, resolves can be a bit tricky sometimes, and they can act strangely with this architecture since resolves don't like controllers that are used outside of the stateProvider call. However, I use resolves in my own app that uses this architecture and it works perfectly fine.

Here's an example:

function userRoutes($stateProvider) {

    var user = {
            name: 'user',
            abstract: true,
            url: '/user',
            template: '<div ui-view></div>',
            controller: 'UsersCtrl'
        },
        userID = {
            name: 'user.id',
            abstract: true,
            url: '/:id',
            template: '<div ui-view></div>',
            controller: 'UsersCtrl',
            resolve: {
                user: function(Restangular, $stateParams) {
                    return Restangular.one('users', $stateParams.id).get();
                }
            }
        };

    $stateProvider.state(user);
    $stateProvider.state(userID);

}

userRoutes.$inject = ['$stateProvider'];
module.exports = userRoutes;

In this case, this is the top level config file for the users module. The main module doesn't contain a Directive or any HTML. That's all handled within the sub-modules. Any sub module controller within the users module can be injected with the "user" object defined in the userID resolve and then all you have to do is write $scope.user = user; in your sub-module controllers and you should be good to go.

Let me know if you run into a problem and I'll see what I can do to help you resolve it. Cheers!

from angular-gulp-browserify-starter.

taylorfort avatar taylorfort commented on June 9, 2024

hmm... so I modeled something pretty similar to what you have there, but I'm running into an issue on the module controller side. let's just use your example there, in your UsersCtrl controller. Would you be adding something like...

function UsersViewCtrl(user) {
    // BLAH BLAH BLAH
}
...
UsersViewCtrl.$inject = ['user'];

If I do this and alert out the value of the resolved variable, I see the result I'm looking for, but my template doesn't render and I get an error (that I'd expect) that it can't find the provider userProvider...

update... as I was writing this, I came across the issue. In my directive for the module, I was setting the controller. Because it was calling a controller in a controller, it was screwing with the $inject and i was getting the "can't find provider" error message. I took out the controller from the directive and everything works fine now. Thanks a ton for your help!

edit...

to illustrate more, here's the part I deleted from the skeleton to make it work in my app. This was found in the app / modules / home / homeDirective.js

controller: 'HomeViewCtrl', // called from homeController.js

from angular-gulp-browserify-starter.

goodbomb avatar goodbomb commented on June 9, 2024

Yeah, sorry, I could have been clearer. When you're using controllers as part of a resolve, the controller you specify can't be connected to the DOM (either via HTML or through a directive). That's why I used the resolve on a top level abstract route using a controller meant specifically for sub-module inheritance. I find that with this application structure, it's generally best to use your resolves at the top module level (as opposed to the sub-module levels) and then inject the resolved object into your sub-module controllers.

Anyway, glad you got it sorted out!

from angular-gulp-browserify-starter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.