Muzammil
Muzammil

Reputation: 117

How to assign values to control using angularjs

I have a detail view and from database only single record is comming, I need to assign data to controls. below is my code

  var app = angular.module("MyProductApp", []);
        app.controller("ProductsController", function ($scope, $http) {
            var ProductID = @Html.Raw(Json.Encode(ViewData["ProductID"]));
            $http.get('/api/Products/GetProductForEditOrDetail',{params: { ProductID : ProductID }}).
            success(function(data){
                $scope.Product = data;
            }).
            error(function(data){
                alert("msg");
            });
        });

and below is html code

<div ng-app="MyProductApp" class="form-horizontal">
    <div ng-controller="ProductsController">
        <h4>Product</h4>
        <hr />
        <div class="form-group" >
            <label class="control-label col-md-2">Product Name</label>
            <div class="col-md-10">

                <input id="txtProductName" type="text" class="form-control" name="txtProductName" disabled="disabled" Value="{{Product.ProductName}}" />
                <input id="hdnProductID" type="hidden" class="form-control" name="hdnProductID" value="{{Product.ProductID}}" />
                <input id="hdnCategoryID" type="hidden" class="form-control" name="hdnCategoryID" value="{{Product.CategoryID}}" />
            </div>
        </div>
</div>
</div>

Upvotes: 1

Views: 79

Answers (3)

Satpal
Satpal

Reputation: 133423

You should use ngModel directive

The ngModel directive binds an input,select, textarea (or custom form control) to a property on the scope using NgModelController, which is created and exposed by this directive.

<input ng-model="Product.ProductName" />

Upvotes: 1

Pankaj Parkar
Pankaj Parkar

Reputation: 136174

You wanted to pass value with hidden fields, so ng-model would not work in that case, you could use ng-value in that case

<input id="hdnCategoryID" type="hidden" class="form-control" name="hdnCategoryID" ng-value="Product.CategoryID" />

Upvotes: 0

macrog
macrog

Reputation: 2105

ngModel is what you are looking for, read the docs !

Upvotes: 0

Related Questions