1

I'm using angular 1.6.1 and angular-ui-router 0.3.2.

I have basic routes set-up as follow:

{
  state: 'home',
  config: {
    url: '/home',
    templateUrl: '...',
    title: 'home'
  }
},
{
  state: 'view',
  config: {
    url: '/view',
    controller: '...',
    controllerAs: 'vm',
    templateUrl: '...',
    title: 'view'
  }
}

this object then gets passed as needed to $routeProvider.state(). in my home state I use $state.go() to switch to the view state. When I'm at the view state my template looks like so:

<div>
  <div class="go-back" ng-click="vm.goBack()">
    <img src="/src/assets/images/back.png"/>
  </div>
  <tabs>
    <tab>
      <directive1></directive1>
    </tab>
    <tab>
    </tab>
    <tab>
    </tab>
  </tabs>
  <directive2></directive2>
  <directive3></directive3>
</div>

Each directive here has a bound controller to it.

My problem is that the directives controller are wrong. What I mean by it is if I inspect for example directive2 html in the inspector, and try to inspect it's controller in chrome using angular.element($0).scope() I will get a scope which is not the correct scope of the directive. Even weirder it's parent scope is the scope of directive3. If I were to remove directive3 from the template than directive2 will start working again, and it will have the correct scope attached to it. If I were to place directive2 under directive3 in the template it will solve this problem as well. Same problem for the <div class="go-back">...</div> element inside the template. It's scope is the correct one and the vm.goBack() function is called, but if I were to remove directive3 from the template then vm.goBack() won't be called anymore (even tho when inspecting the element scope in the console, it is the correct scope). Problem remains for other controller to, not having their correct scope attached to them for some reason and I can't figure out why.

9
  • ui-router uses $stateProvider to configure states and the config property seems strange. Create a plunker that reproduces issue Commented Apr 8, 2017 at 20:43
  • @charlietfl when I said I pass it as needed, I meant that I iterate over this array of object and I pass it like so $routeProvider.state(obj.state, obj.config) Commented Apr 8, 2017 at 20:51
  • but should be using $stateProvider. Create a demo Commented Apr 8, 2017 at 20:52
  • @charlietfl well I tried to make it a simplified version of the problem but I couldn't test it, it seems as app.js in my pluncker won't load, could you take a look ? plnkr.co/edit/XZ9LKbO8JCFwcNGR92qW Commented Apr 8, 2017 at 22:13
  • @Jorayen if you don't mind, I took a look at your plunkr and fixed it up. Perhaps you can continue with the example of your problem? plnkr.co/edit/31eEdliEZle7ckpqWTqo?p=preview Commented Apr 8, 2017 at 23:41

0

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.