Jyina
Jyina

Reputation: 2902

How to access values from angular $scope variable in MVC view?

The below function retrieves data from controller function and saves it to $scope.Details and redirect to payment view.

$scope.pay = function () {

          $http.get('api/Details/123').success(function (data) {
              $scope.Details = data;
          });

          $location.url('/payment');
      }

Also, I am initializing paymentForm scope to have the values from $scope.Details that are retrieved above.

$scope.paymentForm =  {
          FirstName: $scope.Details.FirstName,
          LastName: $scope.Details.LastName,
      PaymentAmount: $scope.Details.PaymentAmount
      };

Controller function:

[HttpGet]
    [Route("api/Details/{id}")]
    public Details Details(int id)
    {
        return new Details()
        {
            FirstName = "Test",
            LastName = "Tester"
        };
    }

Payment View:

<label for="fname">First Name</label>
<input id="firstname" type="text" ng-model="paymentForm.FirstName">
<label for="lname">Last Name</label>
<input id="lastname" type="text" ng-model="paymentForm.LastName">

The payment form does not populate values in first name and last name fields. Can anyone please point out what I am missing here? Thank you!

Upvotes: 1

Views: 1483

Answers (1)

dpnv
dpnv

Reputation: 194

Try adding the $scope.Details = {} just above the $scope.pay function declaration. Then this code should do the job.

Upvotes: 1

Related Questions