3

I want to be able to reuse my ui-router-wired controllers. They currently receive parameters from ui-router resolve to render their templates. Can I reuse those controllers without ui-router?

For example, I do this with ui-router:

.controller('DetailController', function ($scope, detailData) {
    $scope.controllerDetail = detailData + "is awesome!";
})
.config(function ($stateprovider) {
    $stateprovider.state('detailState', {
        resolve: {
            detailData: function () { return "John Doe"; }
        },
        template: '<p>{{ controllerDetail }}</p>',
        controller: 'DetailController'
    }
}

Now, I want to use the same controller to render a fixed sub-panel elsewhere. For example:

<master ng-init='childData="Jane Smith"'>
    <detail ng-controller='DetailController' ng-controller-params="{ detailData : childData">
        <p>{{ controllerDetail }}</p>
    </detail>
</master>

Of course, in practice, there are actually template files, and some functionality in the controller worth de-duplicating. Also, the resolve data in the first example and init data in the second example both arrive over the wire, but in the second example it arrives as a child object of a larger request rather than an individually-navigated item. Also, I assign my controllers in directives rather via HTML attributes.

16
  • 2
    oh ok.. Got it, pls ignore my stupid question. What cant you just resolve it via another state registration (), after all it is a better practice IMHO to instantiate controllers via router rather than instantiating it via ng-controller directive as much as possible. Commented Mar 13, 2015 at 1:52
  • 1
    You do not necessarily need navigation actually, you could create child states with ui.router unlike ngRouter, something like a partial view to isolate a piece of functionality for example. Commented Mar 13, 2015 at 1:56
  • 1
    I'm confused, but I'm investigating. Commented Mar 13, 2015 at 2:01
  • 1
    I once provided an answer to a related (perhaps not exact duplicate) question - see if it helps: stackoverflow.com/a/28870149/968155. Named views, as @PSL suggested, is definitely one approach and perhaps ideal, but this is here for completeness-sake. Commented Mar 13, 2015 at 3:32
  • 1
    @shannon, yes - see the example in plunker in that question. In one instance name variable is passed Commented Mar 13, 2015 at 5:23

1 Answer 1

1

In my case, which is a similar though limited subcase, I had multiple ui-router routes with resolve() defined, and only one place with the controller instantiated in a template via ng-controller, and the value passed to the controller in such case was a known, constant value.

In such case, you can simply do

.value('detailData', 'here goes the default/fallback value')

If you have multiple usages of ng-controller which should have different detailData, you need to find a better solution obviously.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.