Display elements in scope in angularjs page - javascript

Hy everybody, first question here.
I'm learning angularjs and right now i'm stuck on "Controller and Scope" chapter.
I'm trying to search trough an array of objects and show items that match my query; unfortunately my screen stays blank.
Here's my code:
<!DOCTYPE html>
<html ng-app="">
<head>
<title>Search and print with Scope</title>
</head>
<body>
<div ng-controller="Controller">
<input type="search" ng-model="q" placeholder="CPU" />
<ul>
<li ng-repeat="proc in cpu | filter:q">
{{proc.house}} - {{proc.model}}
</li>
</ul>
</div>
<script src="angular.min.js"></script>
<script>
function Controller($scope)
$scope.cpu = [
{ house: 'Intel', model: 'I7' },
{ house: 'AMD', model: 'Ryzen' },
{ house: 'Qualcomm', modello: 'Snapdragon' }
];
</script>
</body>
</html>
What's wrong?
EDIT for EDIT:Just for those who might look at this question, my error was to call ng-app twice, once in the html tag and once in body tag.
EDIT: I'm here again; I modified the code using the given answers, but I still got a search box and a blank screen. I doubt is code related since I also tried to simply copy and paste it in a new file but i got the same result. What could be?
Code is down here:
<!DOCTYPE html>
<html ng-app="">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script>
var app = angular.module('demoApp', [])
app.controller('SimpleController', function($scope) {
$scope.cpu = [{
house: 'Intel',
model: 'I7'
},
{
house: 'AMD',
model: 'Ryzen'
},
{
house: 'Qualcomm',
model: 'Snapdragon'
}
];
});
</script>
<title>Search and print with Scope</title>
</head>
<body ng-app="demoApp">
<div ng-controller="SimpleController">
<input type="search" ng-model="q" placeholder="CPU" />
<ul>
<li>hi</li>
<li ng-repeat="proc in cpu">
{{proc.house}} - {{proc.model}}
</li>
<li>hi again</li>
</ul>
</div>
</body>
</html>

You need to have a angular module and ng-app mentioned as below
DEMO
var app = angular.module('myApp',[])
app.controller('Controller',function($scope){
$scope.cpu = [
{ house: 'Intel', model: 'I7' },
{ house: 'AMD', model: 'Ryzen' },
{ house: 'Qualcomm', modello: 'Snapdragon' }
];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app="myApp">
<head>
<title>Search and print with Scope</title>
</head>
<body>
<div ng-controller="Controller">
<input type="search" ng-model="q" placeholder="CPU" />
<ul>
<li ng-repeat="proc in cpu | filter:q">
{{proc.house}} - {{proc.model}}
</li>
</ul>
</div>
</body>
</html>

First of all you are trying to get the scope data before declaring it, you need to load the script before using ng-repeat, so move the script before the div definition.
Also you are not correctly defining the Controller, it should be defined within an app module, then bind the ng-app and the Controller in your HTML.
<!DOCTYPE html>
<html ng-app="">
<head>
<title>Search and print with Scope</title>
<script src="angular.min.js"></script>
<script>
var app = angular.module('angularApp',[])
app.controller('Controller',function($scope){
$scope.cpu = [
{ house: 'Intel', model: 'I7' },
{ house: 'AMD', model: 'Ryzen' },
{ house: 'Qualcomm', modello: 'Snapdragon' }
];
});
</script>
</head>
<body ng-app="angularApp">
<div ng-controller="Controller">
<input type="search" ng-model="q" placeholder="CPU" />
<ul>
<li ng-repeat="proc in cpu | filter:q">
{{proc.house}} - {{proc.model}}
</li>
</ul>
</div>
</body>
</html>

Related

Unable to get binding value displayed

I started with AngularJS, but am unable to get the desired output. Here's the code.
index.html
<!doctype html>
<html ng-app>
<head>
<script src="scripts/angular.min.js"></script>
<script src="scripts/underscore-min.js"></script>
<script type="text/javascript" src="scripts/todo.js"></script>
<link rel="stylesheet" href="bootstrap/css/bootstrap.css">
</head>
<body>
<div ng-controller="TodoCtrl">
<h2>Total todos: {{totalTodos}}</h2>
<ul class="unstyled">
<li ng-repeat="todo in todos">
{{todo.text}}
</li>
</ul>
</div>
</body>
</html>
todo.js
function TodoCtrl($Scope) {
$Scope.totalTodos = 4;
$scope.todos = [
{ text: 'Learn AngularJS', done: false },
{ text: 'Build an appp', done: false }
];
}
You need to implement the module and controller code for angular(basic example link http://codepen.io/larryjoelane/pen/VeQbrW ). You could place the following code in your todo.js file. I have placed some comment in the code to show additional changes I made to your posted code to make it work.
In the following example you will notice that I place the ng-app attribute inside the oppening tag of a div. This is because I do not have access to the html tag in code pen. The way you are attempting to do it in your html code is correct. The only thing you are missing is the value.
Live Example: http://codepen.io/larryjoelane/pen/WrMZxg
Angular Controller Code:
angular.module("app", []).controller('TodoCtrl', ['$scope', function($scope) {
//changed from $Scope.totalTodos = 4 (syntax error $Scope would be undefined)
$scope.totalTodos = 4;
$scope.todos = [
{ text: 'Learn AngularJS', done: false },
{ text: 'Build an appp', done: false }
];
}]);
You will also need to add an app name to your ng-app attribute.
Example: <html ng-app="app">
Fully corrected HTML:
<!doctype html>
<html ng-app="app">
<head>
<script src="scripts/angular.min.js"></script>
<script src="scripts/underscore-min.js"></script>
<script type="text/javascript" src="scripts/todo.js"></script>
<link rel="stylesheet" href="bootstrap/css/bootstrap.css">
</head>
<body>
<div ng-controller="TodoCtrl">
<h2>Total todos: {{totalTodos}}</h2>
<ul class="unstyled">
<li ng-repeat="todo in todos">
{{todo.text}}
</li>
</ul>
</div>
</body>
</html>
Additional HTML example using ng-bind attribute:
<!--Example using ng-bind-->
<h1>Example using ng-bind<h1>
<h2>Total todos:<span ng-bind="totalTodos"></span></h2>
<ul class="unstyled">
<li ng-repeat="todo in todos" ng-bind="todo.text">
</li>
</ul>
Change this
$Scope
To this
$scope
Also you need
ng-app="app" which is your module name, i believe you haven't defined your module
Index.html
<!doctype html>
<html ng-app="app">
<head>
<script src="//code.angularjs.org/1.4.8/angular.js"></script>
<script src="scripts/underscore-min.js"></script>
<script type="text/javascript" src="scripts/todo.js"></script>
<link rel="stylesheet" href="bootstrap/css/bootstrap.css">
</head>
<body>
<div ng-controller="TodoCtrl">
<h2>Total todos: {{totalTodos}}</h2>
<ul class="unstyled">
<li ng-repeat="todo in todos">
{{todo.text}}
</li>
</ul>
</div>
</body>
</html>
todo.js
angular.module("app", []).controller('TodoCtrl', ['$scope', function($scope) {
$scope.totalTodos = 4;
$scope.todos = [
{ text: 'Learn AngularJS', done: false },
{ text: 'Build an appp', done: false }
];
}]);
Further info you can get here
Using ng-app without a value
Your todo.js contains a casing issue in the second line replace '$Scope' with '$scope'. your code works once I corrected that and include module if you haven't declared already like i mentioned in below code
<!DOCTYPE html>
<html ng-app="exampleApp">
<head>
<script src="scripts/angular.min.js"></script>
<script src="scripts/underscore-min.js"></script>
<script type="text/javascript" src="scripts/todo.js"></script>
<link rel="stylesheet" href="bootstrap/css/bootstrap.css">
</head>
<body>
<div ng-controller="TodoCtrl">
<h2>Total todos: {{totalTodos}}</h2>
<ul class="unstyled">
<li ng-repeat="todo in todos">
{{todo.text}}
</li>
</ul>
</div>
<script>
angular.module("exampleApp",[])
.controller("TodoCtrl",function($scope){
$scope.totalTodos = 4;
$scope.todos = [
{ text: 'Learn AngularJS', done: false },
{ text: 'Build an appp', done: false }
];
});
</script>
</body>

Angular- View Not loading

I wrote a file new.html and I am trying to test navigation. But when i load the page View1.html should be loaded but it shows a blank page.
Here is my new.html:
<!DOCTYPE html>
<html data-ng-app="demoApp">
<head>
<title> Angular Test</title>
</head>
<body>
<div>
<div data-ng-view></div>
</div>
<script src = "Scripts/angular.min.js"></script>
<script src="Scripts/angular-route.min.js"></script>
</body>
<script>
var demoApp = angular.module('demoApp', ['ngRoute']);
demoApp.config(function ($routeProvider) {
$routeProvider
.when('/',
{
controller: 'SimpleCtr',
templateUrl: 'View1.html'
})
.when('/2',
{
controller: 'SimpleCtr',
templateUrl: 'View2.html'
})
.otherwise({ redirectTo: '/' });
});
demoApp.controller('SimpleCtr', function($scope) {
$scope.customers = [
{ name:'Rajat', city:'Kanpur' },
{ name:'Adarsh', city:'Lucknow' },
{ name:'Manoj', city:'Banaras' }
];
$scope.addCustomer = function() {
$scope.customers.push({ name: $scope.newCustomer.name, city: $scope.newCustomer.city });
}
});
</script>
</html>
And here is my View1.html:
<div class="container">
<h2> View 1 </h2>
Name: <br/>
<input type="text" data-ng-model="filter.name"/>
<br/>
<ul>
<li data-ng-repeat = "cust in customers | filter:filter.name | orderBy:'city'"> {{cust.name}} </li>
</ul>
Customer Name: <br/>
<input type="text" data-ng-model="newCustomer.name"/>
<br />
Customer City: <br />
<input type="text" data-ng-model="newCustomer.city"/>
<br />
<button data-ng-click = "addCustomer()"> Add Customer </button>
<a href = "#/2" > View 2 </a>
</div>
Here is View2.html:
<div class="container">
<h2> View 2 </h2>
City:
<br/>
<input type="text" data-ng-model="city" />
<br/>
<ul>
<li data-ng-repeat = "cust in customers | filter:filter.city | orderBy:'city'"> {{cust.name}} </li>
</ul>
</div>
Please help me where i am going wrong?
You are missing a closing bracket in your code on line 8
<!DOCTYPE html>
<html data-ng-app="demoApp">
<head>
<title> Angular Test</title>
</head>
<body>
<div>
<div data-ng-view> </div> <!-- right here -->
</div>
I created a plunker here: http://plnkr.co/edit/uj4S1ybv7vJSsQctG1nD
http://plnkr.co/edit/uj4S1ybv7vJSsQctG1nD
Your views aren't loaded because you try to access them using the file:// protocol. If you put your website on a HTTP server (XAMPP for example) you'll get the desired result.
It works fine with Plnkr. http://plnkr.co/edit/NmfnOMTRHXzsLkcHfgZX?p=preview
My best guess is you are not running the file in HTTP server. The simplest HTTP server is, run this command in your working directory:
python -m SimpleHTTPServer
Then, request your program in browser
localhost:8000/new.html

AngualrJS controller not working

I just began to learn AngularJS by following this youtube video. First part is okay except when it comes to the controller part.
My code is as below (it's the same as in the video)
<html data-ng-app="">
<head>
<script src="angular.min.js"></script>
<script>
function SimpleController($scope) {
$scope.customers = [{
name: 'Kamal',
city: 'York'
}, {
name: 'Sunil',
city: 'DC'
}, {
name: 'Malith',
city: 'Gotham'
}];
}
</script>
</head>
<body>
<div data-ng-controller="SimpleController">Name :
<input type="text" data-ng-model="name" />
</br>
<ul>
<li data-ng-repeat="cust in customers | filter :name | orderBy:'city'">{{cust.name | uppercase}} - {{cust.city}}</li>
</ul>
</div>
</body>
</html>
when I add data-ng-controller="SimpleController" it will not working and give the following error in the console.
Then when I try to post the question in the SO , I tried it in JSfiddle. I added Angular.js and selected onLoad and not working. But when I selected no wrap - in <head> it works fine.
But I can't do that in my local machine so the problem remains as it is.
Can anybody point me to the correct path ?
You need to initialize app:
var app = angular.module("myApp", []);
<div ng-app="myApp" ng-controller="SimpleController">
<!-- ^^^^^ -->
Demo: http://jsfiddle.net/xy23ybzp/2/
Docs: https://docs.angularjs.org/guide/bootstrap
Check Manual Initialization Section in Docs
After getting help from the answers listed here for this question. I got it working. Below is the working code.
<html >
<head>
<script src = "angular.min.js" ></script>
<script>
var app = angular.module("myApp", []);
app.controller("SimpleController",function($scope){
$scope.customers = [
{name :'Kamal',city : 'York'},
{name : 'Sunil',city:'DC'},
{name : 'Malith',city:'Gotham'}
];
});
</script>
</head>
<body >
<div ng-app="myApp" data-ng-controller="SimpleController">
Name :
<input type="text" data-ng-model="name" />
</br>
<ul>
<li data-ng-repeat="cust in customers | filter :name | orderBy:'city'">{{cust.name | uppercase}} - {{cust.city}}</li>
</ul>
</div>
</body>
</html>

why ng-controller is not calling or working or function is not working

<!DOCTYPE html>
<html data-ng-app="myApp">
<head>
<title></title>
<script src="Script/angular.js"></script>
</head>
<body data-ng-controller="SimpleController">
// controller defined //
<ul>
<li data-ng-repeat="data in customers">
// data is not access controller
{{data.name}}-{{data.city}}
</li>
</ul>
</div>
/Is this way is correct to define controller/
<script>
function SimpleController($scope) {
$scope.customers = [
{ name: 'alok ', city: 'azam' },
{ name: 'muku', city: 'lko' },
{ name: 'rajat', city: 'jungle' }
];}
</script>
</body>
</html>
I've rewritten your html a bit and it works now.
You have to define module called myApp and have to use controller directive to define controller in the module.
Please look at the sample I've added
http://jsfiddle.net/uv0gw4kL/2/
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title></title>
</head>
<body ng-controller="SimpleController">
<ul>
<li ng-repeat="data in customers">
{{data.name}}-{{data.city}}
</li>
</ul>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.js"></script>
<script type="text/javascript">
angular.module('myApp', [])
.controller('SimpleController', function ($scope) {
$scope.customers = [
{ name: 'alok ', city: 'azam' },
{ name: 'muku', city: 'lko' },
{ name: 'rajat', city: 'jungle' }
];
});
</script>
</body>
</html>
More about angular controllers here
http://www.w3schools.com/angular/angular_controllers.asp
You can also use allowGlobals feature from controller provider
https://docs.angularjs.org/api/ng/provider/$controllerProvider
but it's not recommended.

Angularjs external templating: the template cannot be loaded?

I thought loading an external template with Angularjs is as simple as this below,
<div ng-include='template/index.php'></div>
But it does not print anything out on the browser. What have I missed?
The html,
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Angualr</title>
<meta charset="utf-8">
<script src="js/angular.min.js"></script>
<script src="js/app.js" type="text/javascript"></script>
<script src="js/maincontroller.js" type="text/javascript"></script>
</head>
<body>
<div ng-include='template/index.php'></div>
</body>
</html>
the template,
<div id='content' ng-app='MyTutorialApp' ng-controller='MainController'>
<input type='text' ng-model='searchText' />
<ul>
<li ng-repeat='person in people | filter:searchText' ng-show='person.live == true'>#{{person.id}} {{person.name}}</li>
</ul>
<input type='text' ng-model='newPerson' />
<button ng-click='addNew()'>Add</button>
</div>
js/app.js,
var app = angular.module('MyTutorialApp',[]);
js/maincontroller.js,
app.controller("MainController", function($scope){
$scope.people = [
{
id: 0,
name: 'Leon',
music: [
'Rock',
'Metal',
'Dubstep',
'Electro'
],
live: true
},
{
id: 1,
name: 'Chris',
music: [
'Indie',
'Drumstep',
'Dubstep',
'Electro'
],
live: true
}
];
$scope.newPerson = null;
$scope.addNew = function() {
if ($scope.newPerson != null && $scope.newPerson != "") {
$scope.people.push({
id: $scope.people.length,
name: $scope.newPerson,
live: true,
music: [
'Pop',
'RnB',
'Hip Hop'
]
});
}
}
});
EDIT:
Directories,
index.html
js/
...
...
template/
index.php
EDIT 2:
index.html,
<div ng-app='MyTutorialApp'>
<div ng-include='template/index.php'></div>
</div>
template/index.php,
<div id='content' ng-controller='MainController'>
<input type='text' ng-model='searchText' />
<ul>
<li ng-repeat='person in people | filter:searchText' ng-show='person.live == true'>#{{person.id}} {{person.name}}</li>
</ul>
<input type='text' ng-model='newPerson' />
<button ng-click='addNew()'>Add</button>
</div>
Live demo here (click).
ng-include looks for a $scope property, so you need to pass it a string, like this: ng-include="'/template/index.php'".
<div ng-include="'/template/index.php'"></div>
What you were passing to it essentially makes it look for this in your controller: $scope['/template/index.php'] = 'some string';
You're also bootstrapping angular in the template itself - so how could it be included? ng-app needs to be in the main page so that ng-include can work!
<some-element ng-app="myApp">
<!-- in here, angular things work (assuming you have created an app called "myApp" -->
<div ng-include="'/template/index.php'"></div>
</some-element>
Just replace some-element with something like html, body or whatever element you want the app to work from.
You bootstrapped your ng-app in the template, but you have to bootstrap it in your main page.
So just move the ng-app directive from the template to the main-page, e.G.
<html ng-app="MyTutorialApp">
<div ng-include src='template/index.php'></div>
try this
and add ng-app into top of the page
<html ng-app='MyTutorialApp'>
you must have bootstrap you angular application into your index.html

Categories

Resources