Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
693 views
in Technique[技术] by (71.8m points)

angularjs - Recursive ui router nested views

I am trying to create a file viewer and I want to nest the subdirectories. I am using ui-router and I want each subdirectory to have its own URL and state.

Say I have the following structure:

Root
  |__Folder
  |__Folder
     |__SubFolder
        |__SubSubFolder

I want my routes to be:

files/:folderID/:SubFolderID/:SubSubFolderID

And I would like to do that recursively as opposed to creating a new state for each subdirectory

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would suggest, do it with one state and one param - folderPath. Because ui-router should have all the states defined soon enough, to support url routing. All these unique folderPath could differ, could be dynamic - in the runtime, in app life time.

Dynamic state definition is always an issue (if states are defined in app.run() it could happen that user comes to url which is not defined yet - otherwise is used... bad)

Dynamic url parameter - will work always. We just have to parse it in controller and decide next steps. Here is a working example.

The state and its param could be like this

.state('files', {
      url: '/files/{folderPath:[a-zA-Z0-9/]*}',
      templateUrl: 'tpl.files.html',
      controller: 'FileCtrl'
    });

Later we can dynamically generate navigation (links) like this:

<a href="#/files/Folder1">
<a href="#/files/Folder1/SubFolder1/">
<a href="#/files/Folder1/SubFolder1/SubFolderA">
<a href="#/files/Folder1/SubFolder1/SubFolderB">
<a href="#/files/Folder1/SubFolder2/SubFolderX">

check that in this example


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...