0

My app.js look like

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
$scope.eventmetrics = [
                        { 
                          _id: '57d439cf666a8d1080003e90',
                           firstName: [ 'name', 'text' ],
                           lastName: [ 'last', 'text' ],
                           taginvitations: ['first,second,third', 'text'],
                           userId: 1,
                           tagcontact: ['file,second,third,fourth', 'text'] 
                         }
                     ]
     }); 

i want print array of tag1 and tag2 in list example

invitations

  • first
  • second
  • thrid

contact

  • first
  • second
  • thrid
  • fourth
4
  • Where do you want to print? Commented Sep 11, 2016 at 5:23
  • my json is come from database and i want print dynamically instead of using static tag1 and tag2 Commented Sep 11, 2016 at 5:59
  • Then assign tag1 to be whatever your server returns Commented Sep 11, 2016 at 6:03
  • can anyone give solution for this problem? Commented Sep 11, 2016 at 12:44

3 Answers 3

1

According to the contents of the array tag1: ['first, second, third', 'text'], tag1 contains only two elements, then you must separate the content of the first element to obtain a new matrix that you need to present.

Use: tag1[0].split(',').

This demo works with more than one element in the matrix eventmetrics.

Something like this:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.eventmetrics = [{
    _id: '57d439cf666a8d1080003e90',
    firstName: ['name', 'text'],
    lastName: ['last', 'text'],
    tag1: ['first,second,third', 'text'],
    userId: 1,
    tag2: ['file,second,third,fourth', 'text']
  }, {
    _id: '87d439cf666a8d1080003e55',
    firstName: ['name', 'text'],
    lastName: ['last', 'text'],
    tag1: ['first,second,third', 'text'],
    userId: 1,
    tag2: ['file,second,third,fourth, fifth', 'text']
  }]
});
#metrics {
  border: solid 1px #f00;
  margin: 2px;
  padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="plunker">
  <div ng-controller="MainCtrl">
    <div id="metrics" ng-repeat="em in eventmetrics">
      <h3 ng-bind="('_id: ' + em._id)"></h3>

      <div>
        <h4>tag1</h4>

        <ul>
          <li ng-repeat="tag1 in em.tag1[0].split(',')" ng-bind="tag1"></li>
        </ul>
      </div>
      <div>
        <h4>tag2</h4>

        <ul>
          <li ng-repeat="tag2 in em.tag2[0].split(',')" ng-bind="tag2"></li>
        </ul>
      </div>
    </div>
  </div>
</div>

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

3 Comments

but my json is come from database and i want print dynamic instead of using static tag1 and tag2
@Danny Fardy Jhonston Very nice example. It works for me.
This is my plunker link and i mention the problem in comment of html file ...so please refer it ......plnkr.co/edit/Trc1EP4A9ei9qM7YYWyB?p=preview
0

Assuming the eventmetrics is always have length == 0, you can something like this using ng-repeat:

<ul>
  <li ng-repeat="text in eventmetrics[0].tag1"> {{ text }} </li>
</ul>


<ul>
  <li ng-repeat="text in eventmetrics[0].tag2"> {{ text }} </li>
</ul>

Comments

0

Check out the complete code in Plunker:

https://plnkr.co/edit/3TLG27?p=preview

<!DOCTYPE html>
<html ng-app="plunker">
 <head>
  <link rel="stylesheet" href="style.css">
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
  <script src="script.js"></script>
 </head>
 <body ng-controller="MainCtrl">
   <ul ng-repeat="y in eventmetrics">
    <div ng-repeat="(key,value) in y" ng-show="key=='contact'||key=='invitation'">
    <b>{{key}}</b>
     <li ng-repeat="x in value[0].split(',')">
      {{x}}  
     </li>
    </div>
   </ul>
 </body>
</html>

//script.js

var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
  $scope.eventmetrics = [
                    { 
                      _id: '57d439cf666a8d1080003e90',
                       firstName: [ 'name', 'text' ],
                       lastName: [ 'last', 'text' ],
                       invitation : ['first,second,third', 'text'],
                       userId: 1,
                       contact: ['file,second,third,fourth', 'text'] 
                     }
                 ];
    }); 

7 Comments

but my json is come from database and i want print dynamic instead of using static tag1 and tag2
Refer my same plunker again. If this is what you are looking for.
ya it is work but still it is static...please go again my question because i did edit now ...and thank you for previous suggestion.
This is work if I put namespace as a tag before the contact and invitation but at a view side i also want print the invitation and contact so what can i do?
Check the code in plnkr, it prints contact and invitation as well.
|

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.