Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to target specific ui-view elements when nested ui-views exist

Imagine some html as follows:

<body ng-app="blocksApp">

Some content goes here

<div ui-view="monty">

    <div ui-view="dave">Aaa</div>
    <div ui-view="pete">Bbb</div>
    <div ui-view="steve">Ccc</div>

</div>

</body>

Using ui-router, is there any way to code a state that will set "dave" to a new snippet of html, whilst leaving everything else untouched.

e.g. I'd like to do this:

$stateProvider
        .state('daveonly',{
            url: "/dave",
            views:{
                'dave':{template:"Dave now has content"}
            }
        })

I can't get it to work. The reason I want to do this is that sometimes I'd like to replace 'Dave' with a partial update, other times I'd like to replace the entire 'monty' with a partial update. It seems that ui-router does not like having nested ui-views in the same snippet of html.

like image 454
whatdoesitallmean Avatar asked May 18 '16 15:05

whatdoesitallmean


2 Answers

From one point of view I'd like to suggest:

  • move html code to '.tpl.html' files
  • use 'templateUrl' instead of 'template'

And check if the following is suitable for you:

$stateProvider.state("daveonly", {
    views: {
        "dave": {
            templateUrl: 'daveonly.tpl.html',
        },
        "pete": {
            templateUrl: 'pete.tpl.html',
        },
        "steve": {
            templateUrl: 'steve.tpl.html',
        },        
    }    
});

Take a look at page1 and page2 for more details.

But from another point of view it could be more useful to use only one ui-view and to redesign current ui-views to become the appropriate directives with controllers/services: usage of directives with controllers/services could help to manage partial reload correctly and to write unit-tests.

like image 77
marbug Avatar answered Oct 17 '22 08:10

marbug


Yes it can be done easily with the help of abstract states and yes you are correct ui-router doesn't like direct nested views directly but it works fine if the views are in any child template.

Now consider this main page(index.html)

<body ng-app="app">
 <div ng-view=""></div>
</body>

Now this template which will appear in this unnamed view. (parent.html)

<h3>This is the parent template</h3>
<div ng-view="child1"></div>
<div ng-view="child2"></div>

Now the JS file

$stateProvide.state('home',{
  url:'/',
  abstract:true,
  views:{
    "":{
      templateUrl:'parent.html'
    }
  }
})
.state('home.child',{
  url:"",
  views:{
    'child1@home':{
      templateUrl:'child1.html'
    },
    'child2@home':{
      template:'Child2'
    }
  }
})
.state('home.child.child1',{
  url:"[email protected]",
  views:{
    'child1@home':{
      templateUrl:'child1viewchange.html'
    }
  }
});

(Now the manipulation part)

(child1.html)

<button ui-sref="home.child.child1">Child</button>

Now child1viewchange.html pe jana padega and wo dekhne wali hai kaunsi kaisi thi/...... (child1viewchange.html)

<h3>Child1's view change</h3>

So now when we click on the button in child view1 the content in the first view changes and if we assign controllers then they can use them to control data.

like image 34
Rohan Avatar answered Oct 17 '22 09:10

Rohan