Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I want to use angular scope variable in anchor tag ng-click as param[that param should be used with @string.Concat] in cshtml page like below.

              <a href="#" 
  ng-click="GetName('@string.Concat("Selected name:",name)')"> {{name}} </a>

var personApp = angular.module('personApp', []);
personApp.controller('personController', ['$scope', function ($scope) {
    $scope.GetName = function(name){
      alert(name);
    }
}]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<html>
<body ng-app="personApp">
    <h1>Scope</h1>
    <div  ng-controller="personController" ng-init="names=['John Doe', 'Mary Jane', 'Bob Parker']">
        <ul>
            <li ng-repeat="name in names">
              <a href="#" 
  ng-click="GetName('@string.Concat("Selected name:",name)')"> {{name}} </a>
            </li>
        </ul>
    </div>

</body>
</html>

share|improve this question
up vote 1 down vote accepted

@string.Concat is C# code and it gets executed on the server when razor tries to render the page. At that time your angular code/variable is not even available ! Razor will send the output of it's execution (of the C# code in the view) to the browser which is plain html markup. Then only your angular code will be executed. So you cannot mix client side js varibale to c# code like that.

The best you can do is to simply pass the name variable which is your client side js variable.

<li ng-repeat="name in names">
    <a href="#" ng-click="GetName(name)"> {{name}} </a>
</li>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.