AngularJS module not defined - javascript

I just got a book called "Pro Angular" from Apress by Adam Freeman. I have never used Angular JS before and I am not really sure why this isn't working but I'll explain where I am currently. Ok, so the first project I am currently doing is a basic to do list using Angular to bind certain data to html tags using data-binding and I've just added a line defining a module. Problem is my environment is claiming that the module is not being defined. I'm not sure what it is talking about. Here is my source so far.
<!DOCTYPE html>
<html ng-app="todoApp">
<head>
<title>TO DO List</title>
<link href="bootstrap.css" rel="stylesheet" />
<link href="bootstrap-theme.css" rel="stylesheet" />
<script src="angular.js"></script>
<script>
var model = {
user: "Adam",
items: [{ action: "Buy Flowers", done: false },
{ action: "Get Shoes", done: false },
{ action: "Collect Tickets", done: true },
{ action: "Call Joe", done: false }]
};
var todoApp = angular.module("todoApp", []);
todoApp.controller("ToDoCtrl", function ($scope) {
$scope.todo = model;
});
</script>
</head>
<body ng-controller="ToDoCtrl">
<div class="page-header">
<h1>
{{todo.user}}'s To Do List
<span class="label label-default">{{todo.items.length}}</span>
</h1>
</div>
<div class="panel">
<div class="input-group">
<input class="form-control" />
<span class="input-group-btn">
<button class="btn btn-default">Add</button>
</span>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>Description</th>
<th>Done</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in todo.items">
<td>{{item.action}}</td>
<td><input type="checkbox" ng-model="item.done" /></td>
<td>{{item.done}}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
The line it is talking about is Ln-18. Any help with an explanation would be greatly appreciated.

Related

How do I get my new constructor to work? I'm using Javascript in Visual Studio Code

My JS code looks like this. But when I open the page it says "Uncaught TypeError: Cannot set property 'innerHTML' of null"
I tried to change the NewItem constructor to a function. But VSC keeps saying I should declare it as a class instead. I converted it in the first place because the constructor wasn't working as a function either.
class NewItem {
constructor(name, date, price) {
this.itemName = name;
this.itemDate = date;
this.itemPrice = price;
}
}
const onListItem = new NewItem("John", "Date", "Price");
document.getElementById("demo").innerHTML = onListItem.itemDate;
HTML looks like this
<!DOCTYPE html>
<html lang= "en">
<head>
<link rel="stylesheet" href="ShopListStyle.css">
<meta name="viewport" content="width=device-width" initial-scale=1.0>
<title>Shopping List</title>
</head>
<body>
<script src="ShopListScript.js"></script>
<div id="container">
<div id="inputbox" class="section">
<form id="labels-form">
<label><h2>Shopping List</h2></label>
<input id="labels-name" class="labels-input" type="text" value="Item Name"></input>
<input id="labels-date" class="labels-input" type="text" value="Date of Purchase"></input>
<input id="labels-price" class="labels-input" type="text" value="Item Price"></input>
<button id="labels-button">Add to List</button>
<p id="demo"></p>
</form>
</div>
<div id="shopListBox" class="section">
<!--Need to add a delete button/ Maybe a quantity button down the road-->
<table id="shopList">
<caption><h2>Spending List</h2></caption>
<thead id="table-header">
<tr>
<th class="table-header" id="table-name">name</th>
<th class="table-header" id="table-date">date</th>
<th class="table-header"id="table-price">price</th>
<th class="table-header" id="table-delete">delete</th>
</tr>
</thead>
<tbody id="table-body">
<tr class="newRow">
<td class="new-item" id="item-name">item</td>
<td class="new-item" id="item-date">item</td>
<td class="new-item" id="item-price">item</td>
<td class="new-item"><button class="item-button">Delete</button></td>
</tr>
</tbody>
<tfoot id="table-footer">
<tr>
<td id="item-price-sum" colspan="4" style="width: 100%" >Sum of Prices</td>
</tr>
</tfoot>
</table>
<!--The sum of all the prices will go somewhere here-->
</div>
</div>
</body>
</html>
Your JavaScript code is trying to access this element with id demo (<p id="demo"></p>):
document.getElementById("demo").innerHTML = onListItem.itemDate;
Your script is added at the opening body tag...
<body>
<script src="ShopListScript.js"></script>
...
...which means the demo element does not exist yet.
Solution: Put your script before the closing body tag:
...
<script src="ShopListScript.js"></script>
</body>
</html>
You can also try to set
<script src="ShopListScript.js" defer="defer"></script>
Because javascript will block the DOM rendering, so we should put in the end of
like Peter Krebs's answer:
...
<script src="ShopListScript.js"></script>
</body>

ng-repeat data binding in Angular not working. Where am I going wrong?

I'm trying to make a simple Premier League table using a free football data api. When I console log vm.table, I get all the data I should have for the table to work. Does that mean there is a fault in the html file? I'm a total novice with angular and I'm trying to learn it by making this little app. Can someone point me to where my problem is? The console shows no errors or anything, but it simply doesn't print the data into the table as expected.
this is the html file:
<html ng-app="eplApp" lang="en">
<head>
<meta charset="UTF-8">
<title>EPL Feeder</title>
<!-- styles -->
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.css">
<link rel="stylesheet" href="node_modules/font-awesome/css/font-awesome.css">
<link rel="stylesheet" href="style.css">
<!-- scripts -->
<script src="node_modules/angular/angular.js"></script>
<script src="node_modules/jquery/dist/jquery.js"></script>
<script src="script.js"></script>
</head>
<!-- define angular controller -->
<body ng-controller='tableCtrl as table'>
<table>
<thead>
<tr><th colspan="4">English Premier League Table</th></tr>
<tr>
<td>Pos</td>
<td>Team</td>
<td>Played</td>
<td>Win</td>
<td>Draw</td>
<td>Loss</td>
<td>GF</td>
<td>GA</td>
<td>GD</td>
<td>Points</td>
</tr>
</thead>
<tbody>
<tr ng-repeat='team in vm.table' ng-class="{top:team.position === 1, cl: (team.position > 1) && (team.position < 5), el:team.position === 5, rel: (team.position > 17)}">
<td>{{team.position}}</td>
<td class="flexbox" ng-click="teamView()">
<img ng-src="{{team.crestURI}}" alt="{{team.teamName}} crest" />
<p class="teamName">{{team.teamName}}</p>
</td>
<td><p>{{team.playedGames}}</p></td>
<td>{{team.wins}}</td>
<td>{{team.draws}}</td>
<td>{{team.losses}}</td>
<td>{{team.goals}}</td>
<td>{{team.goalsAgainst}}</td>
<td>{{team.goalDifference}}</td>
<td>{{team.points}}</td>
</tr>
</tbody>
</table>
This is the script file:
var app = angular.module('eplApp', []);
app.controller('tableCtrl', function($http) {
var vm = this;
vm.table = [];
$http({
headers: { 'X-Auth-Token': '971acba677654cdb919a7ccebd5621e2' },
method: "GET",
url: "http://api.football-data.org/v1/soccerseasons/426/leagueTable"
}).then(function(response) {
vm.table = response.data.standing;
console.log(vm.table);
});
});
You should change,
From
<body ng-controller='tableCtrl as table'>
To
<body ng-controller='tableCtrl as vm'>
it should be similar to this because you use controllerAs syntax in your app.
<tr ng-repeat='team in table.standing' ...
Edit
after editing your question you should use #Sajeetharan answer.

Angularjs bootstrap popover blink after model change

I am using angularjs bootstrap popover inside a table.
The table is populated using ng-repeat and if the popover is open and i get new data to the table there is a flicker to the popover.
Here is a working example
Pknkr
Any thought on how can i prevent the flicker ?
<!doctype html>
<html ng-app="ui.bootstrap.demo">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.0.0.js"></script>
<script src="example.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div ng-controller="PopoverDemoCtrl">
<br/>
<br/>
<table>
<tr ng-repeat="item in list">
<td>
<button uib-popover-template="dynamicPopover.templateUrl"
popover-is-open="aux.openPopOverId==item.id"
ng-click="aux.openPopOverId=item.id" popover-placement="right" type="button" class="btn btn-link">
Popover
</button>
</td>
</tr>
</table>
<script type="text/ng-template" id="myPopoverTemplate.html">
<div>{{dynamicPopover.content}}</div>
<div class="form-group">
<label>Popup Title:</label>
<input type="text" ng-model="dynamicPopover.title" class="form-control">
</div>
</script>
</div>
</body>
</html>
And the js
angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('PopoverDemoCtrl', function ($scope,$interval, $sce) {
$scope.dynamicPopover = {
templateUrl: 'myPopoverTemplate.html',
title: 'Title'
};
$scope.list = [{'id':1},{'id':2},{'id':3}];
$scope.aux = {'openPopOverId':2};
$scope.updateList = function(){
$scope.list = [{'id':1},{'id':2},{'id':3}];
}
$interval($scope.updateList,500,0);
});
I thought this could be help you please read here in detail..

Why ng-repeat is not working?

ng-repeat not working with table,in the output only header part displayed?
As i think the binding i did is perfectly fine, but something is there which i am missing?
Can anyone help me out where i am doing wrong?
JAVA SCRIPT:
var myapp=angular.module("MyApp",[]);
var controller=function($scope)
{
var technology1=[
{Name: "C#",Likes: 0,Dislikes: 0},
{Name: "JAVA",Likes:0,Dislikes:0},
{Name: "Python",Likes:0,Dislikes:0}
];
$scope.technology=technology1;
$scope.incrementLikes=finction(technology)
{
technology.Likes++;
}
$scope.discrementLikes=function(technology)
{
technology.Dislikes++;
}
}
myapp.controller('MyController',controller);
<html ng-app="MyApp">
<head>
<title></title>
<script src="angular.js"></script>
<script src="Day2.js"></script>
</head>
<Body ng-controller="MyController">
<div >
<table border='2'>
<thead>
<tr>
<th>Name Of Technology</th>
<th>Likes</th>
<th>Dislikes</th>
<th>Likes/Dislikes</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="tech in technology">
<td>{{tech.Name}}</td>
<td>{{tech.Likes}}</td>
<td>{{tech.Dislikes}}</td>
<td>
<input type="button" value="Like" ng-click="incrementLikes(tech)">
<input type="button" value="Dislikes" ng-click="decrementLikes(tech)">
</td>
</tr>
</tbody>
</table>
</div>
</Body>
</html>
Replace this line
$scope.incrementLikes=finction(technology)
by
$scope.incrementLikes=function(technology)
Your code has a typo in your myController controller. Change finction to function.
As Pankaj Parkar pointed out you need to correct the "finction" typo as well as to reference the $scope.technology.Likes and $scope.technology.dislikes when you are incrementing their values.
So update these lines:
$scope.incrementLikes=finction(technology)
{
technology.Likes++;
}
$scope.discrementLikes=function(technology)
{
technology.Dislikes++;
}
To this
$scope.incrementLikes=function(technology)
{
$scope.technology.Likes++;
}
$scope.discrementLikes=function(technology)
{
$scope.technology.Dislikes++;
}
Here's the fully corrected code. I can't comment on the answer from #pzelenovic but do NOT add "$scope.technology.Likes++;" or "$scope.technology.Likes++;" to your increment/decrement functions. Those are fine the way they are because you're updating the likes/dislikes property on the "tech" object you passed in from the click function.
var myapp=angular.module("MyApp",[]);
var controller=function($scope)
{
var technology1=[
{Name: "C#",Likes: 0,Dislikes: 0},
{Name: "JAVA",Likes:0,Dislikes:0},
{Name: "Python",Likes:0,Dislikes:0}
];
$scope.technology=technology1;
$scope.incrementLikes=function(technology)
{
technology.Likes++;
}
$scope.decrementLikes=function(technology)
{
technology.Dislikes++;
}
}
myapp.controller('MyController',controller);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app="MyApp">
<head>
<title></title>
<script src="angular.js"></script>
<script src="Day2.js"></script>
</head>
<Body ng-controller="MyController">
<div >
<table border='2'>
<thead>
<tr>
<th>Name Of Technology</th>
<th>Likes</th>
<th>Dislikes</th>
<th>Likes/Dislikes</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="tech in technology">
<td>{{tech.Name}}</td>
<td>{{tech.Likes}}</td>
<td>{{tech.Dislikes}}</td>
<td>
<input type="button" value="Like" ng-click="incrementLikes(tech)">
<input type="button" value="Dislikes" ng-click="decrementLikes(tech)">
</td>
</tr>
</tbody>
</table>
</div>
</Body>
</html>

Routing issues with Angular JS

I can't for the life of me figure out what I'm doing wrong. I'm going off a John Linquist video doing some basic AngularJS routing. When I don't have my table in a partials folder and in my index.html page (and not using ng-view so I don't have to configure routing, it works fine. However, I try to inject my a partial view of my table with <ng-view></ng-view> then I get the error: http://goo.gl/nZAgrj (from the angular docs)
app.js
angular.module('enterprise',[])
.config(function($routeProvider){
$routeProvider.when("/",{templateUrl:"/partials/list.html"})
})
//this is the that iterated over in the partial view's ng-repeat
function AppController($scope){
$scope.crew =[
{name:'Picard',description:'captain'},
{name: 'Riker',description:'number 1'},
{name:'Word',description:'Security'}
]
}
index.html
<!DOCTYPE html>
<html>
<head>
<title>Angular JS Routing</title>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"/>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css" rel="stylesheet">
</head>
<body>
<div ng-app="enterprise" ng-controller="AppController">
<h2>Enterprise Crew</h2>
<ng-view></ng-view>
//list.html should be injected here
</div>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script>
<script src="js/app.js"></script>
</body>
</html>
list.html
<table class="table table-striped" style="width: 250px">
<thead>
<tr>
<td>Name</td>
<td>Description</td>
<td><i class="glyphicon glyphicon-plus-sign"></i> </td>
</tr>
</thead>
<tbody>
<tr ng-repeat="person in crew">
<td>{{person.name}}</td>
<td>{{person.description}}</td>
<td><i class="glyphicon glyphicon-edit"></i></td>
</tr>
</tbody>
</table>
You need to include ng-route.
angular.module('myApp', ['ngRoute']).
cdn:
http://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-route.min.js
And is it working like that?
angular.module('enterprise',[])
.config(function($routeProvider){
$routeProvider.when("/",{templateUrl:"/partials/list.html"})
})
.controller("AppController", ['$scope',
function ($scope){
$scope.crew =[
{name:'Picard',description:'captain'},
{name: 'Riker',description:'number 1'},
{name:'Word',description:'Security'}
];
}
]);
Ok, response is here : AngularJS 1.2 $injector:modulerr

Categories

Resources