1

We are use this approad to append an Id in AngularJS

.when(
            '/fundingRequirements/:id',
            {
                templateUrl : '/views/dashboard/crowdFunding/planningFunding.html',
                controller : 'planningFunding'
            })

And then in Controller used this

$location.path("/fundingRequirements/"+data.data)

Now the problem occur that Firstly it is save then Id is generated at Server .So in this URL there is not an Id in URL , and in second case it is edit , so at that time there is the requirement of Id in URL . Does we need to make another URL or there is any approach to use same URL in both cases ??

Note - HTML Form , controller , server site all methods are same for both operation i.e. save, edit

2 Answers 2

1

Following Angular's documentation, you can perfectly achieve that by defining all of your routes. Indeed Angular seems to evaluate the longest route from the definitions compared to the actual URL.

You can find here my modifications of Angular's example plunker that illustrate that.

  .when('/Book', {
    templateUrl: 'bookroot.html',
    controller: 'BookController'
  })

I just added this route, an arbitrary bookroot.html template and a link to /Book at the top of the main template. You'll see that the link works, as well as the other ones.

In order to sum up, this example contains the following combined parametered urls:

  • /Book
  • /Book/:bookId
  • /Book/:bookId/ch/:chapterId
Sign up to request clarification or add additional context in comments.

4 Comments

but still we have to define two TemplateURL can we do something like localhost:8080/?id=null localhost:8080/?id=1234 In this case same url work . Something like that we want
Well you still can use the same templateUrl, but you'll either have to use separate controllers or to use the same controller that will make a check on parameters you use in $routeParams (in case your id is empty for example). However, I still recommend to use separate views if you are going to different content display. If there is common content, you may think about using a common directive or including a common template.
We create the common Template , in which if call is for Save then it save into DB . And if user want to edit then automatically a method is call to get data from DB fill the form . Same template is used in both cases
Then same template, same controller, different routes. The controller will have to interpret creation mode from the undefined id parameter and you're done :)
0

From what i gather you want to redirect two different views for same url. You might want to use ui-router for this purpose. It is an advanced library for angular routing providing a whole lots of features over angular's default ng-route service.

You can try something like this:

$stateProvider.state('saveOrEdit', {
url:'/fundingRequirements/:id',
templateUrl: ['$stateParams', function ($stateParams) {
    var id = $stateParamas.id
    if (id) {
          return //Template URL for edit state
     } else {
          return //Template URL for save state;
     }
  ]
})

P.S: I'm not sure but this should also work for ng-route also.

4 Comments

I want some thing like that .when( '/fundingRequirements/id:id', { templateUrl : '/views/dashboard/crowdFunding/planningFunding.html', controller : 'planningFunding' })
your redirect url is same in this case. All you have to do is provide different template url.
Template url is also same , (we use same form in both cases)
If template url is same then there shouldn't be any problem. You can fetch the id in the URL and put it in the controller then can render the different views based on that. Your actual problem is not clear in the question. You might wanna review it.

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.