Not able to loop values in angularjs - javascript

I have a code and I want to loop throught the array in json format by using ng-repeat and ng-init.
But the code isn't working.
Below is the following code.
<!DOCTYPE html>
<html ng-app="">
<head>
<title></title>
</head>
<body>
<h1>
ng - repeat
</h1>
<hr />
<div ng-init="myFavLan=[{name:'PHP',extension:'.php'},
{name:'Javascript',extension:'.js'},
{name:'HTML',extension:'.html'}
]">
<p ng-repeat="language in myFavlan">
Name : {{ language.name }} <br />
Extension : {{ language.extension }}
</p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</body>
</html>
Please note that I am a beginner in angularjs

Javascript is case-sensitive, so the variable myFavlan you have in the ng-repeat is undefined. Just change
<p ng-repeat="language in myFavlan">
to
<p ng-repeat="language in myFavLan">
You could also change your ng-init variable.

It should be myFavLan Change
From
<p ng-repeat="language in myFavlan">
To
<p ng-repeat="language in myFavLan">
DEMO
<!DOCTYPE html>
<html ng-app="">
<head>
<title></title>
</head>
<body>
<h1>
ng - repeat
</h1>
<hr />
<div ng-init="myFavLan=[{name:'PHP',extension:'.php'},
{name:'Javascript',extension:'.js'},
{name:'HTML',extension:'.html'}
]">
<p ng-repeat="language in myFavLan">
Name : {{ language.name }} <br />
Extension : {{ language.extension }}
</p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</body>

Related

I can't reach an element from HTML by using Javascript

I want to create a project with using colors.
All of my files are in the same folder.
I can't reach the my color input that is in the index.html by:
const firstColor = document.getElementById("color1").value;
I can just reach elements which is in the game.js.
Also my canvas is in the game.html.
I have added tags into all of my HTML files.
My index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Cyber Bird</title>
<link rel="stylesheet" href="main.css">
</head>
<body align="center">
<div id="title">Cyber Bird</div>
<div id="mainMenu">
<div id="selectColors">
<div id="colorsTitle">SELECT <span>2</span> COLORS FOR YOUR <span id="cb">CYBER BIRD</span></div> <br> <hr> <br>
<span id="first"> First: <input type="color" name="color1" id="color1" value="#f1aabc"></span>
<span id="second"> Second: <input type="color" name="color2" id="color2" value="#2d96b9"></span>
</div>
START
</div>
<script src="src/game.js"></script>
</body>
</html>
My game.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Cyber Bird</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div id="container" align="center">
<div id="scoreBoard">
<div id="header">SCOREBOARD</div>
<div id="description">Score: </div>
<div id="score">0</div>
</div>
<canvas id="myCanvas" height="500" width="900"></canvas>
<div class="helper" id="toJump">press <span>SPACE</span> to jump</div>
<div class="helper" id="toStart">click <span onclick="startGame()">HERE</span> to start</div>
<div class="helper" id="toSelect">select new <span>COLORS</span></div>
</div>
<div id="darken"></div>
<div id="gameOver" align="center">
<div id="message">GAME OVER</div>
<div id="yourScore">Your score is: <span id="finalScore"></span></div>
<div id="tryAgain" class="button" onclick="startGame()">Try Again</div>
</div>
<script src="game.js"></script>
</body>
</html>
You're not passing the data from your index.html to game.html. So when you call this:
START
You're loading a new page, which removes all of your existing variables - including 'color1'.
I see 2 options here:
Make this a single-page web app. So when you click your START button, it hides the index div and loads a game div
Save your values from index.html somewhere (browser storage?) and then call that after you load your new game.html page.

how to render html script in reactjs

<li><a id="customPopover">Data Source One</a>
<i class="fa fa-times"></i>
<div class="axis_popover left">
<div class="axis_popover_title">Title</div>
<div class="axis_popover_content">
<p><input type="text" class="form-control box_ip"/></p>
<p><button class="btn">button</button></p>
</div>
</div>
having script like this
having html code like this and i need to render in react.
You can refer this example that will run using ReactJS. It will give you basic idea about ReactJS.
<!DOCTYPE html>
<html lang="en">
<head>
<!-- use react js 15.0 js -->
</head>
<body>
<div id="showHere"></div>
<script type="text/babel">
var Example = React.createClass({
render: function() {
return ( <div>
<a id="customPopover">Data Source One</a>
<i class="fa fa-times"></i>
<div class="axis_popover left">
<div class="axis_popover_title">Title</div>
<div class="axis_popover_content">
<p><input type="text" class="form-control box_ip"/></p>
<p><button class="btn">button</button></p>
</div>
</div>
</div>)
}
});
ReactDOM.render(
<div>
<Example></Example>
</div>,
document.getElementById('showHere') );
</script>
</body>
</html>

AngularJS ng-repeat from json string

I am trying to find tasks from a project and display them in separate div .
Following json string is my data source which is within {{projects}} variable.
[{"_id":"200566","value":[{"name":"Inbox","pinned":false,"milestone-id":"","description":"","uncompleted-count":0,"id":"713888","complete":false,"private":false,"position":3999,"status":"reopened","projectId":"200566","projectName":"fastfy.com","DLM":null},{"name":"General tasks","pinned":false,"milestone-id":"","description":"","uncompleted-count":6,"id":"673437","complete":false,"private":false,"position":4000,"status":"reopened","projectId":"200566","projectName":"fastfy.com","DLM":null}]},{"_id":"221840","value":[{"name":"Inbox","pinned":false,"milestone-id":"","description":"","uncompleted-count":0,"id":"800864","complete":false,"private":false,"position":3998,"status":"reopened","projectId":"221840","projectName":"The Crown","DLM":null},{"name":"General tasks","pinned":true,"milestone-id":"","description":"","uncompleted-count":3,"id":"751194","complete":false,"private":false,"position":4000,"status":"reopened","projectId":"221840","projectName":"The Crown","DLM":null}]},{"_id":"203859","value":[{"name":"General tasks","pinned":false,"milestone-id":"","description":"","uncompleted-count":3,"id":"690722","complete":false,"private":false,"position":4000,"status":"reopened","projectId":"203859","projectName":"mphosipalityconsulting.com","DLM":null}]},{"_id":"207043","value":[{"name":"General tasks","pinned":false,"milestone-id":"","description":"","uncompleted-count":1,"id":"700757","complete":false,"private":false,"position":4000,"status":"new","projectId":"207043","projectName":"Gloucester B&B & Pub","DLM":null}]}]
I have tried
{{projects}}
<li class="list-group-item" ng-repeat="p in projects">
<div class="nm">
{{p.value[$index].projectName}}
</div>
<div class="taskBtn">
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#taskList{{post.id}}">[+]</button>
</div>
<div id="taskList{{post.id}}" class="collapse">
{{p.value[$index].name}}
</div>
</li>
This outputs following
Could you tell me how do i get the looping properly and get Something like
Project name :
Task lists :
project name :
tasks lists:
Project name :
Task lists :
project name :
tasks lists:
Use ng-repeat inside an ng-repeat
Your DOM
<!DOCTYPE html>
<html ng-app="App">
<head lang="en">
<meta charset="utf-8">
<title>Custom Plunker</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body ng-controller="Proj">
<ul ng-repeat="p in projects">
<ul>
<li ng-repeat="item in p.value">
{{item.name}}
</li>
</ul>
</ul>
</body>
</html>
Controller code:
angular.module('App', []).controller('Proj', function ($scope) {
$scope.projects = [{"_id":"200566","value":[{"name":"Inbox","pinned":false,"milestone-id":"","description":"","uncompleted-count":0,"id":"713888","complete":false,"private":false,"position":3999,"status":"reopened","projectId":"200566","projectName":"fastfy.com","DLM":null},{"name":"General tasks","pinned":false,"milestone-id":"","description":"","uncompleted-count":6,"id":"673437","complete":false,"private":false,"position":4000,"status":"reopened","projectId":"200566","projectName":"fastfy.com","DLM":null}]},{"_id":"221840","value":[{"name":"Inbox","pinned":false,"milestone-id":"","description":"","uncompleted-count":0,"id":"800864","complete":false,"private":false,"position":3998,"status":"reopened","projectId":"221840","projectName":"The Crown","DLM":null},{"name":"General tasks","pinned":true,"milestone-id":"","description":"","uncompleted-count":3,"id":"751194","complete":false,"private":false,"position":4000,"status":"reopened","projectId":"221840","projectName":"The Crown","DLM":null}]},{"_id":"203859","value":[{"name":"General tasks","pinned":false,"milestone-id":"","description":"","uncompleted-count":3,"id":"690722","complete":false,"private":false,"position":4000,"status":"reopened","projectId":"203859","projectName":"mphosipalityconsulting.com","DLM":null}]},{"_id":"207043","value":[{"name":"General tasks","pinned":false,"milestone-id":"","description":"","uncompleted-count":1,"id":"700757","complete":false,"private":false,"position":4000,"status":"new","projectId":"207043","projectName":"Gloucester B&B & Pub","DLM":null}]}];;
});
http://plnkr.co/edit/4RaxuzA3YXRIG8LvPwlr?p=preview
This Nested ng-repeat can b your solution...
<li class="list-group-item" ng-repeat="p in projects">
<div class="nm">
{{p.value[$index].projectName}}:
<ul ng-repeat="t in p.value">
<li>{{t.name}}</li>
</ul>
</div>
<div class="taskBtn">
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#taskList{{post.id}}">[+]</button>
</div>
<div id="taskList{{post.id}}" class="collapse">
{{p.value[$index].name}}
</div>
</li>

ng-click doesn't affect parent's children

Why does this not really work?
I have a field test.
But when I click on the <p> of child1 it will not affect the child2 <p>.
<div ng-init="test=false">
<div id="child1">
<p ng-click="test=!test">
Click!
</p>
</div>
<div id="child2">
<p ng-if="test">
Clicked..
</p>
</div>
</div>
You need to define an angular module in JS and assign it using the ng-app directive. Like this:
<!DOCTYPE html>
<html ng-app="sample">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script>
angular.module('sample', []);
</script>
</head>
<body>
<div ng-init="test=false">
<div id="child1">
<p ng-click="test=!test">
Click!
</p>
</div>
<div id="child2">
<p ng-if="test">
Clicked..
</p>
</div>
</div>
</body>
</html>
Every directive creates its own scope.
So,if you want to access the variable/object in parent scope, you need use $parent
<div ng-init="test=false">
<div id="child1">
<p ng-click="$parent.test=!$parent.test">
Click!
</p>
</div>
<div id="child2">
<p ng-if="$parent.test">
Clicked..
</p>
</div>
</div>

Angular : ng-repeat not working

Here is my code , i take it from w3school.
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<script type="text/javascript" src="./angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName = "sparsh";
$scope.lastName = "khandelwal";
});
</script>
<title>Home Page</title>
</head>
<body>
<div ng-app="myApp" ng-init="name='Sparsh'">
<div ng-controller="myCtrl">{{firstName}}</div>
<p>
Name : <input type="text" ng-model="name">
</p>
<h1>Hello {{name}}</h1>
</div>
<div ng-app="" ng-init="names=['Jani','Hege','Kai']">
<ul>
<li ng-repeat="x in names">{{x}}</li>
</ul>
</div>
</body>
</html>
ng-repeat in second block is not working.
here is the output i get on browser
Please help me out
Angular only bootstraps the first found application on the page, i.e. the first container with ng-app attribute. The second one you put, ng-app="" will be ignored. Although, you could manually bootstrap the second one with angular.bootstrap method, in your case it makes more sense to wrap entire body into the same app and remove the second ngApp directive:
<body ng-app="myApp">
<div ng-init="name='Sparsh'">
<div ng-controller="myCtrl">{{firstName}}</div>
<p>
Name :
<input type="text" ng-model="name" />
</p>
<h1>Hello {{name}}</h1>
</div>
<div ng-init="names=['Jani','Hege','Kai']">
<ul>
<li ng-repeat="x in names">{{x}}</li>
</ul>
</div>
</body>
Demo: http://plnkr.co/edit/UXTGEWOaRu82WpeOvuOz?p=preview
you can have only one ng-app in html page
so change your code as
<body ng-app="myApp">
<div ng-init="name='Sparsh'">
<div ng-controller="myCtrl">{{firstName}}</div>
<p>
Name :
<input type="text" ng-model="name" />
</p>
<h1>Hello {{name}}</h1>
</div>
<div ng-init="names=['Jani','Hege','Kai']">
<ul>
<li ng-repeat="x in names">{{x}}</li>
</ul>
</div>
</body>
The problem is, that only one ng-app can be automatically bootstrapped per webpage. So the second ng-repeat does not get parsed because it is not contained within an app.
You need to manually bootstrap your apps with angular.bootstrap()
https://docs.angularjs.org/api/ng/function/angular.bootstrap
This part of your code
<div ng-app="" ng-init="names=['Jani','Hege','Kai']">
<ul>
<li ng-repeat="x in names">{{x}}</li>
</ul>
</div>
does not have an ng-app
ng-repeat only works within an ng-app

Categories