html not rendered in ng-template angular - angularjs

I have some jsons data such as below, when I tried to print them recursively with ng-include and ng-template (as shown below), the ng-include's inside html codes doesnot rendered and it is blank.
and other problem is that in printing ng-include , it just print 1 level of recursion and it doesn't keeping on printing.
so what's wrong?
Json data:
[{"name":"default","path":">var>www>rapa-v2>themes>","subdir":[{"name":"css","path":">var>www>rapa-v2>themes>>default","subdir":[{"name":"images","path":">var>www>rapa-v2>themes>>default>css","subdir":[]},{"name":"style.css","path":">var>www>rapa-v2>themes>>default>css"}]},{"name":"index.blade.php","path":">var>www>rapa-v2>themes>>default"},{"name":"information.json","path":">var>www>rapa-v2>themes>>default"},{"name":"screen.jpg","path":">var>www>rapa-v2>themes>>default"}]}]
Html code:
<div class="theme-files-list ltr" ng-init="loadDir('{{ $theme }}')">
<script type="text/ng-template" id="directoryTree">
<div class="tree-item">
<i class="fa fa-file-text-o maroon"></i>
<a href="<% '/admin/setting/template/edit/{{ $theme }}/'+item.name %>">
<% item.name %>
</a>
</div>
<ul ng-if="item.subdir">
<li ng-repeat="item in item.subdir" ng-include="'directoryTree'"></li>
</ul>
</script>
<ul class="noselect">
<li>
<div class="tree-item bg-aqua">
<i class="fa fa-paint-brush blue"></i>
{{ ucfirst($theme) }}
</div>
<ul>
<li ng-repeat="item in themeDir" ng-include="'directoryTree'"></li>
</ul>
</li>
</ul>
</div>
important: I use laravel and for blade system print, I have replaced angular print symbols {{ }} with <% %>

I think you have to use different code. Your json represent a tree of folder but in the angular app, you're only iterating on the first level and in the first level you have only 1 item with subdir.
If you format correctly your json response you can easily understand this.
[
{
"name":"default",
"path":">var>www>rapa-v2>themes>",
"subdir":
[
{
******** You're targeting this element below **********
"name":"css",
"path":">var>www>rapa-v2>themes>>default",
"subdir":
[
{
"name":"images",
"path":">var>www>rapa-v2>themes>>default>css",
"subdir":[]
},
{
"name":"style.css",
"path":">var>www>rapa-v2>themes>>default>css"
}
]
},
{
******** This doesn't has subdir **********
"name":"index.blade.php",
"path":">var>www>rapa-v2>themes>>default"
},
{
******** This doesn't has subdir **********
"name":"information.json",
"path":">var>www>rapa-v2>themes>>default"
},
{
******** This doesn't has subdir **********
"name":"screen.jpg",
"path":">var>www>rapa-v2>themes>>default"
}
]
}
]
So you have to change the json or to change ng-repeat

Related

html not rendered in ng-template

I have some json with correct format that passed from laravel controller and i want to show them recursively with angular but the HTML inside ng-template not rendered. what's wrong?
note: I replaced the angular default tag {{ }} with <% %>
<div id="treeViewContent" ng-init="getTreeData()">
<script type="text/ng-template" id="directoryTree">
<div class="tree-item" ng-click="loadDir(dir.address)">
<i class="fa fa-folder orange"></i>
<% dir.name %>
</div>
<ul ng-if="dir.subdir">
<li ng-repeat="dir in dir.subdir" ng-include="'directoryTree'"></li>
</ul>
</script>
<ul class="noselect">
<li>
<div class="tree-item" ng-click="loadDir('/')">
<i class="fa fa-hdd-o blue"></i>
{{ ucfirst(\Auth::user()->username) }}
</div>
<ul>
<li ng-repeat="dir in dirs" ng-include="'directoryTree'"></li>
</ul>
</li>
</ul>
</div>
and getTreeData() is:
$scope.dirs = [];
$scope.getTreeData = function()
{
$http.post("/admin/drive/treeData", {_token: csrf_code}).then(function(response)
{
if (response.status == 200)
{
$scope.dirs = response.data;
}
else
{
Flash({status: "failed", message: "Error"});
}
})
}
and json data
[
{
"name":"image",
"address":"\/image",
"subdir":[
{
"name":"document",
"address":"\/image\/document",
"subdir":[
]
}
]
},
{
"name":"document",
"address":"\/document",
"subdir":[
]
},
{
"name":"movies",
"address":"\/movies",
"subdir":[
]
}
]
Please find the plunkr link plnkr.co/edit/B33ZhzU03CyS45a0lX4w?p=preview, the above code is working fine. Please check the json structure or post the json here.

Angular expression in ng-repeat and ng-click

I am trying to call a function using ng-click which is dynamically generated by ng-repeat the problem is that i am not able to get the parameter value passed in ng-click function. My html code is
<div class="col-md-4 col-xs-12 col-lg-4" ng-repeat="(key, value) in uploadedFiles">
<b>value.filename is {{value.filename}}</b>
<img ng-if="value.filetype=='jpg'" ng-src="http://localhost:3000/uploads/{{value.filename}}" class="img img-responsive thumbnail uploadedfile-thumb"/>
<img ng-if="value.filetype=='PDF'" ng-src="images/pdf-thumb.jpg" class="img img-responsive thumbnail uploadedfile-thumb"/>
<a ng-href="http://localhost:3000/uploads/{{value.filename}}" target="_blank" class="uploaded-file-links">View</a>
<a ng-href="#" ng-click="deleteFile(value.filename);" class="uploaded-file-links">Delete</a>
</div>
i am not getting the filename which is passed in deleteFile(), bit it is working in <b> tag in 2nd line so finally this is a problem with my angular expression so how should i write it?
I will try to make some guessing here.
I suppose that your uploadedFiles is an array of objecs, and not an object itself. In that case, you should use ng-repeat="file in uploadedFiles" which is meant to iterate over array elements. This will iterate over your array and assignf the corresponding object for that iteration to file variable.
The ng-repeat="(key, value) in uploadedFiles" is meant for iterating over object properties instead of iterating over array elements. For example, if uploadedFiles is:
var uploadedFiles = {
prop1: 'value1',
prop2: 'value2',
prop3: 'value3'
};
and you iterate over it like this:
<ul>
<li ng-repeat="(key, value) in uploadedFiles">
{{value}}
</li>
</ul>
you would get the following output:
value1
value2
value3
But if uploadedFiles is an array like this one:
var uploadedFiles = [
{
prop1: 'value11',
prop2: 'value12',
},
{
prop1: 'value21',
prop2: 'value22',
},
{
prop1: 'value31',
prop2: 'value32',
}
];
And you iterate over it like this:
<ul>
<li ng-repeat="file in uploadedFiles">
{{file.prop1}}
</li>
</ul>
you would get:
value11
value21
value31
So in your case, I would try the following:
<div class="col-md-4 col-xs-12 col-lg-4" ng-repeat="file in uploadedFiles">
<b>file.filename is {{file.filename}}</b>
<img ng-if="file.filetype=='jpg'" ng-src="http://localhost:3000/uploads/{{file.filename}}" class="img img-responsive thumbnail uploadedfile-thumb"/>
<img ng-if="file.filetype=='PDF'" ng-src="images/pdf-thumb.jpg" class="img img-responsive thumbnail uploadedfile-thumb"/>
<a ng-href="http://localhost:3000/uploads/{{file.filename}}" target="_blank" class="uploaded-file-links">View</a>
<a ng-href="#" ng-click="deleteFile(file.filename);" class="uploaded-file-links">Delete</a>
</div>

How to show object in array angular?

How to show comments in items?
This is where items save
$scope.items = [
{ 'item': 'one',
'comments':[{'comment':'comment'}]
},
{ 'item': 'two',},
{'item': 'three'}
];
<ul>
<li ng-repeat="ite in items">
{{ite.item}} {{ite.comments.length}}
<button ng click="remove($index)">Remove</button> <div ng-repeat="c in ite.comments">{{c.comment}}</div>
</li>
</ul>
http://plnkr.co/edit/19w1Q3XhoWQcpxm5SuxX?p=preview
You Just missed to add your comment Div in between the list tag. please update the code like above code. I have checked on Plunker.
<li ng-repeat="ite in items">
{{ite.item}} {{ite.comments.length}}
<button ng-click="remove($index)">Remove</button> <div ng-repeat="c in ite.comments">{{c.comment}}</div></li>

How to use ng-repeat when we need to display two array one is normal string another is image

I have two array which is given below
$scope.base64Array=[null, null, "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQ…cmnAgj0PvTTgZJ5osK4w8dahc1MTnOMcnionwR2JNKwXP/9k=", null, null, null];
$scope.products=["Wheat", "LCD Monitor", "Ethernet Cable", "Optical Mouse", "Rice", "Mac Mini RAM 4 GB"];
Here I have an null value in base64array if it is null then no need to display image and if i have base64 string i need to display in that image row
and my ionic view looks like
<ul class="list">
<li class="item" ng-repeat="i in products" ui-sref="leadProduct" >
{{i}}
<br>
<img ng-show="imgURI === undefined" ng-src="http://placehold.it/100x100">
</li>
</ul>
You can add a track by $index to the repeat so you can tell which index you are at then index into base64Array.
<ul class="list">
<li class="item" ng-repeat="i in products track by $index" ui-sref="leadProduct" >
{{i}}
<br>
<img ng-show="base64Array[$index] != undefined" ng-src="{{'data:image/png;base64,'+base64Array[$index]}}">
<img ng-show="base64Array[$index] == undefined" src="http://placehold.it/100x100">
</li>
</ul>
Try process this two arrays before assignment, loop the base64Array, remove the null item and the same index item in products.
var tmp_base64Array = ["/9j/4AAQSkZJRgABAQAAAQABAAD..."];
var tmp_products = ["Ethernet Cable"];
After process the two arrays would be like this, then assignment to $scope.
edit:
$scope.products = [
{ "name": "Wheat", "imgUrl": null },
{ "name": "LCD Monitor", "imgUrl": "/9j/4AAQSkZJRgABA..." }
];
<ul class="list">
<li class="item" ng-repeat="product in products" ui-sref="leadProduct" >
{{product.name}}
<img ng-src="{{product.imgUrl || 'http://placehold.it/100x100'}}">
</li>
</ul>

Use nested ng-repeat outside the element

everyone, I've a scenario like this...
$scope.flags = {
'porIdade': {
'text': 'Por idade',
'flags': {
'meses1a3': '1 a 3 meses',
'meses4a7': '4 a 7 meses',
'meses8a12': '8 a 12 meses'
}
},
'sexo': {
'text': 'Sexo',
'flags': {
'menino': 'Menino',
'menina': 'Menina'
}
},
'estacao': {
'text': 'Coleção',
'flags': {
'menino': 'Inverno',
'menina': 'Verão'
}
}
}
Basically I need to put, for example: porIdade.text into the panel-heading into a DIV
and populate the children inside the UL. I don't know how to use ng-repeat for this. This is the html.
<div class="panel panel-default" id="leftColumn">
<div class="panel-heading">PARENT</div>
<ul class="list-group">
<li class="list-group-item">Child1</li>
<li class="list-group-item">Child1</li>
<li class="list-group-item">Child1</li>
<li class="list-group-item">Child1</li>
</ul>
</div>
so, if I set something like this:
PARENT
this will create 3 rows with panel-heading. So, to solve this I need something like...
<ng-repeat start here (key,val) in flags>
<div class="panel panel-default" id="leftColumn">
<div class="panel-heading">PARENT</div>
<ul class="list-group">
<li class="list-group-item" ng-repeat children>{{child.text}}</li>
</ul>
</div>
</ng-repeat finish here>
ANy suggestions ? I can't use a DIV before the panel :( so, this suggestions is not valid. It's causes a weird behaviour with the bootstrap.
You need to use a nested ng-repeat like this: (I've removed classes to keep the concept more readable)
<div ng-repeat="data in flags">
<div ng-bind="data.text"></div>
<ul>
<li ng-repeat="item in data.flags" ng-bind="item"></li>
</ul>
</div>

Resources