abhaygarg12493
abhaygarg12493

Reputation: 1605

Append Id in url in angularjs

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

Upvotes: 1

Views: 2288

Answers (2)

ngasull
ngasull

Reputation: 4216

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

Upvotes: 1

saurabhgupta05085
saurabhgupta05085

Reputation: 68

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.

Upvotes: 0

Related Questions