0

I am getting a form data from json.I have binded all data into a form fields, but Resource field data coming as separate object and I want to show it as comma separated.
I tried this,But its showing two resouce fields. How to do it?

Here is my code

<form ng-repeat="data in editProjDetails">
                    <div>
                        <label class="displayBlock">Project Name</label>
                        <label><input type="text" ng-model="data.ProjectName" ng-disabled="all"></label>
                    </div>
                    <div>
                        <label class="displayBlock">Client</label>
                        <label><input type="text" ng-model="data.Client" ng-disabled="all"></label>
                    </div>
                    <div id="projectCoOrdBlock">
                        <label class="displayBlock">Project Co-ordinator</label>
                        <label><input type="text" ng-model="data.ProjectCoordinator" ng-disabled="true"></label>
                    </div>
                    <div id="resourceBlock">
                        <label class="displayBlock">Resource</label>
                        <label><input type="text"  ng-repeat="x in data.ResourceAllocated"  ng-model="x.ResourceName"  ng-disabled="true"></label>
                    </div>                  
</form>

I did this in jquery.This is what I am trying to implement in angular

 rAllo = data.ResourceAllocated;
        if (rAllo.length == 0) {
            $("#resource").val("No resources available");
        }
        else {
            for (var i = 0; i < rAllo.length; i++) {
                var arrayDatas = rAllo[i].ResourceName;
                resource.push(arrayDatas)
            }
            $("#resource").val(resource)
        }

2 Answers 2

1
$scope.resourceAllocated= $scope.data.ResourceAllocated.map(function(el){return el.name}).join(",");

 <input type="text" ng-model="resourceAllocated">

Try using this

<!DOCTYPE html>
<html lang="en-US">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<input type="text" ng-model="resourceAllocated">
		
</div>

</body>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {

$scope.yearbtn = [
        {year:'2022'},
        {year:'2021'},
        {year:'2019'},
        {year:'2018'},
        {year:'2017'},
        {year:'2016'},
        {year:'2015'},
 ];
 
 $scope.resourceAllocated= $scope.yearbtn.map(function(el){return el.year}).join(",");

});
</script>
</html>

Working Demo

Sign up to request clarification or add additional context in comments.

4 Comments

it returns this error Cannot read property 'ResourceAllocated' of undefined
check whether you are getting data inside $scope.data.ResourceAllocated or provide a plunker
console log your $scope.data.ResourceAllocated and check whether data is present and do not write resource allocated input field inside ng-repeat in HTML
why we are passing el here?
1

Try it like this,

<div id="resourceBlock"  ng-repeat="x in data.ResourceAllocated">
     <label class="displayBlock">Resource</label>
     <label><input type="text"   ng-model="x.ResourceName"  ng-disabled="true"></label>
</div>   

DEMO

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) { 
$scope.data =[{"Id":1010,"ProjectName":"aaa","Client":"aaa","ProjectCoordinator":["RajkumarS‌​"],"OnsiteCoordinator":"aaa","ResourceAllocated":[{"ResourceName":"RajkumarS","Re‌​sourceId":9,"RoleName":"Manager"},{"ResourceName":"aSd","ResourceId":1012,"RoleNa‌​me":"tester"}],"ProjectRoles":null}];
 $scope.resourceAllocated= $scope.data[0].ResourceAllocated.map(function(el){return el.ResourceName}).join(",");

});
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello AngularJS</title>
</head>

<body ng-controller="myCtrl">     
 
     <label class="displayBlock">Resource</label>
     <label><input type="text"   ng-model="resourceAllocated" ></label>
 
 <script src="https://code.angularjs.org/1.6.1/angular.js"></script>
 </body>
</html>

5 Comments

you mean 3 inputs?
yes.I have two resources in my json.so its showing two resources in two input fields.
thats what ng-repeat does, what else you expect?
I want to show all my resourceName in a single input field with comma separated.
check the demo above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.