1

I am using angular 1.5 and UI route.

Due to structure limitation we are loading under single state both filters view and content view(mostly for dataset presentation).

I'm trying to achieve single-scope for multiple views, it means: I want my filter view to be able trigger functions from loaded controller on content view.

My state deceleration looks like this:

.state('somestate', {
    url: '/somestate',
    views : {
        "content" : {
            templateUrl: 'dataset.html',
            controller: "someController"
        },
        "filters" : {
            templateUrl: 'filters.html',
        }

    }

})

I want to be able to trigger functions from filters.html, something like this

<div ng-click="doSomething()"></div>

Considering doSomething() deceleration made inside someController

7
  • Possible duplicate of stackoverflow.com/questions/9293423/… Commented Oct 5, 2016 at 11:42
  • @GangadharJannu it is a different issue. I don't want to have 2 controllers, I want to use single controller(1 scope) for multiple views. Commented Oct 5, 2016 at 11:44
  • You can get the instance of 'someController' in filters.html and call the function accordingly Commented Oct 5, 2016 at 11:46
  • I want to achieve clean solution. how would you suggest to get the instance of 'someController' ? Commented Oct 5, 2016 at 11:48
  • If you want to achieve clean solution then go for service/factory. You should move the common functionality in controllers to service/factory and call the same in your respective controllers Commented Oct 5, 2016 at 11:58

2 Answers 2

1

I'm not sure to understand your problem ... If you want to use the same controller, just do it ...

This way :

.state('somestate', {
    url: '/somestate',
    views : {
        "content" : {
            templateUrl: 'dataset.html',
            controller: "someController"
        },
        "filters" : {
            templateUrl: 'filters.html',
            controller: "someController"
        }

    }

})

or using ng-controller in your template file.

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

2 Comments

This way I would have 2 instances of someController. I want to use only 1 single instance (shared scope)
Could you add a snippet to your original post ?
0

Use "controller as" syntax.

If the controller is declared in the state:

controller: 'SomeController`,
controllerAs: 'some'

If inside an html tag:
ng-controller="SomeController as some"

Then inside your templates you can access the controller's scope as some.someFunc()

3 Comments

I have already tried it. I'm not sure why it didn't work, maybe it does not because filter's view is inside other directive, so if we lookup scopes encapsulation we will find that filters is somewhere a child -> child -> child of the content view.
So what exactly are you trying to achieve? It's unclear
1 controller initialized by view1 and view2 is only a template loaded which triggers methods from shared controller. single controller's scope shared on multiple views

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.