codeandcloud
codeandcloud

Reputation: 55333

Using razor syntax and angular

Is it possible to use AngularJS variable as Route Parameter in Razor Syntax like this?

<tr data-ng-repeat="job in jobs">
    <td>
        <a href="@Url.RouteUrl("editJob", new { id =  job.JobID })">
            <i class="glyphicon glyphicon-edit" style="color:#808080"></i>
        </a>
    </td>
</tr>

Right now I am using this.

<a href='@Url.Content("~/job/edit/"){{job.JobId}}'>

Upvotes: 3

Views: 5659

Answers (2)

iJungleBoy
iJungleBoy

Reputation: 5638

I recommend neither solutions because mixing multiple templates is always a bit dangerous. The way our team solves this is

  1. pass in all razor dependencies on in a separate script block with a dynamically created constant on in the ng-init (if only used in simple cases)
  2. only use these in your angular template

A bit like this (pseudo-code, example using ng-init)

<div ng-app="..." ng-init="config = { rootUrl: '@Url.Content("~/job/edit/")', otherValue: ''}">

  <a href="{{config.rootUrl + job.JobId}}">...</a>
</div>

Upvotes: 1

Omri Aharon
Omri Aharon

Reputation: 17064

I'd say no, since Razor is rendered server side and at that time Angular variables don't exist. Meaning new { id = job.jobID } will result in { id : null }.

Your second solution is working because the angular string is concatenated right after the Razor part ends.

Upvotes: 5

Related Questions