Simple angular binding - javascript

I did a very little application, but binding not working as model to object. How is it possible?
angular.module("simpleapp", [])
.controller("Controller", ['$scope', function($scope) {
$scope.sample = {};
$scope.sample.input = 4;
}]);
<body ng-app = "simpleapp" >
<input ng-model="sample.input" type="text" value="text" />
<div ng-controller = "Controller"><p>input "{{sample.input}}"</p></div>
</body>

You are using simpleapp in HTML and just app in your JS.
They have to be the same in order to work correctly.
Plus you put the input with the scope variable outside the controller. It should be inside.
For example you can edit your html like this:
<body ng-app="app" ng-controller="Controller">
<!-- other divs here -->
</body>

You have to update your body tag from <body ng-app="app"> and then you have put put your input inside the scope of your controller
<body ng-app="app">
<div ng-controller = "Controller">
<input ng-model="sample.input" type="text" value="text" />
<p>input "{{sample.input}}"</p>
</div>
</body>

The answers by Atul,NV Prasad and fbid are absolutely correct.You can view the DEMO here.
One thing that they have missed out is another way to bind your elements to the angular variable.That is using ng-bind.It is used as follows:
<p ng-bind="sample.input"></p>
What's the difference between ng-bind="sample.input" and
{{sample.input}}?
When you load the page, with the {{}} notation, you will see the curly brackets until the angular content is rendered.In the case of ng-bind, you won't.
For more details:LINK and this answer specifically

Use Proper Angularjs Version Library .You need to Go through Angularjs Doucmentation and Try to Understand by experimenting . That Makes you Strong
Some Beginner Mistakes :-
You Should write all the code after assigning Controller to Html
Element . Otherwise Your Code Don't Work even if it is Correct . You need
to assign a variable to Use.
html Code sample (understand Your mistake by taking look at code or the Below Plunkers :-) :-
<body ng-controller = "sampleController">
<input ng-model="sample.node" type="text" />
<p>input "{{sample.node}}"</p>
<input ng-model="sample1" type="text" />
<p>input "{{sample1}}"</p>
</body>
Controller Coding :-
angular.module("sampleApp", [])
.controller("sampleController", ['$scope', function($scope) {
$scope.sample = {'node':1};
$scope.sample1 = 'my first default value';
}]);
Here we go Simple Binding Example :-
http://jsfiddle.net/cmckeachie/Y8Jg6/
Plunker With Little More Concept of Data binding :-
https://plnkr.co/edit/TmAsSCKQDHfXYEbCGiHW?p=preview
Go through Documentation for some more understanding Good Luck !!

Related

How to compile HTML?

In controller bind same data like $scope.name = "<div>geetha teko</div>", this name will now binded to html like {{name}}, it is printing like this in browser."<div>geetha tek0</div>", how can get only 'geetha tek0' in browser with html tags.
I tried bellow please help any one.
html code
<div>
<p> my name is: {{name}}</p>
</div>
angularjs
<script>
function MyCtrl($scope) {
$scope.html = "<div> ramesh bogandla</div>"
}
</script>
you can check on jsfiddle:http://jsfiddle.net/ADukg/10127/
I updated your JSFiddle, http://jsfiddle.net/ADukg/10129/.
Use the following:
<span ng-bind-html-unsafe="html"></span>
Or for what you have in your question rather than the JSFiddle you provided:
<p> my name is: <span ng-bind-html-unsafe="html"></span></p>
Here's some further information that might be handy.

How do I add Angular functionality to newly-added HTML?

I have an HTML page with Angular. I want to click a button that adds an Angular input tag to the DOM. But when I do so, the new input text field does not seem to work the way I would expect it.
My HTML looks like this:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0-rc.2/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
var app= angular.module("myApp",[]);
app.controller("myCtrl",function($scope){
});
$(function(){
$("#button").click(function(){
$("body").append("<input type=\"text\" ng-model=\"bar\"><br>{{bar}}");
});
});
</script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<button id="button">click me</button>
<input type="text" name="something" ng-model="foo"><br>
{{foo}}
</body>
</html>
I can use Angular's functions just fine when with the input tag already there. When I type something in the text field, the output appears. But when I click the button and the new input text field comes up, typing in it does nothing.
What am I missing?
How do I fix this?
You can't mix JQUERY and Angular in the way you are doing it. You have to choose one framework and stick to its principles.
In order to add a control the "View" in angular you could use "Directives". Another options is to have an object in your Model and the chances to this object will interact with your controller and this will inform the View to add another control.
Angular is an MVC framework therefore you should not manipulate the View(html) through another framework. Angular was meant to fix and simplify this manipulations with a proper separation or layers.
You can refer to this other post on how to perform this:
How to dynamically add input rows to view and maintain value in angularjs ng-model as array item
To do it in AngularJS:
<body ng-app="myApp" ng-controller="myCtrl">
<button id="button" ng-click="showBarInput=true">click me</button>
<div ng-show="showBarInput">
<input type="text" ng-model="bar"><br>
{{bar}}
</div>
<input type="text" name="something" ng-model="foo"><br>
{{foo}}
</body>
The AngularJS framework is a Model-View-Whatever framework. The model drives the view. In this case, $scope.showBarInput controls the visibility of the elements.

Inserting text into HTML from AngularJS

In my index.html file, I have created a very bare-bones form. I have separated much of the detail into app.js so that if someone wants to have their version of the form look different, they just have to insert a modified copy of app.js (trying to go for portability here).
However, I am very new to AngularJS, which is what a lot of app.js is using. I could use some help please!
Here is a code snippet that I'm having some trouble with:
<body ng-controller="controller">
<div class="page">
<section class="signin">
<form name="form" novalidate>
<div class="intro">
<label ng-model="service-desk-name"></label>
<span ng-bind="service-desk-name"></span>
<label ng-model="welcome"></label>
<span ng-bind="welcome"></span>
</div>
Coupled with:
var app = angular.module('app',[]);
app.controller('controller', ['$scope', function($scope) {
$scope.service-desk-name = 'Arbitrary Service Desk Name';
$scope.welcome =
'Please sign in and a consultant will be with you shortly.';
}]);
Because I may be doing something dreadfully wrong, I'll explain what I want to do. The intro section is displayed above the actual form, showing the service desk name, and some kind of custom welcome or informational message through ng-model="service-desk-name" and ng-model="welcome", respectively. My hope is that I can get the HTML to grab value of these two models that I define in app.js. This way, the HTML doesn't control what is displayed, it just grabs what is defined and displays it.
Right now, these fields are not showing up at all on the webpage, indicating that there's just some kind of syntax error. I'm so new to Angular so I don't really know what to try to fix it from here.
replace
<label ng-model="service-desk-name"></label>
with
<label>{{service-desk-name}}</label>
ng-model means that you want bi-directional binding, and is used for example with inputs, so that when you start typing into the input, the value is stored inside anything that you reference through ng-model, i.e.
<input type="text" ng-model="inputValue" />
will save the input into $scope.inputValue
{{}} means you want to output something from the controller in the view.
Variable name can not have most special character (except _, $) when you are defining directly after a .(dot). This is the reason where controller code is throwing an error, since nothing is getting rendered on page.
You could define that property by specifying key over $scope object like
$scope['service-desk-name'] = 'Arbitrary Service Desk Name';
Additionally the ng-model would work on input element only, but you are using over label. Do below change to welcome as well.
<input ng-model="service-desk-name"/>
But Ideally you shouldn't be define scope variable in such format.
Preferred way would be to use camelCase while defining variables.
Agree with Pankaj Parkar
Also you can't get value in this case because you don't set "ng-app"
plnkr link
var app = angular.module('app', []);
app.controller('myController', function ($scope) {
$scope.serviceDeskName = 'Arbitrary Service Desk Name';
$scope.welcome = 'Please sign in and a consultant will be with you shortly.';
});
<!DOCTYPE html>
<html ng-app="app"> <!-- For example define your app module name here -->
<head>
<link rel="stylesheet" href="style.css">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-controller="myController">
<div class="page">
<section class="signin">
<form name="form" novalidate>
<div class="intro">
<label ng-model="serviceDeskName"></label>
<span ng-bind="serviceDeskName"></span>
<hr>
<label ng-model="welcome"></label>
<span ng-bind="welcome"></span>
</div>
</form>
</section>
</div>
</body>
</html>

Calling Controller in Angular js

I have just started learning Angular js from w3school.com.
I have a simple question about controller.
As this is my code.
<html>
<body>
<div ng-app="" ng-Controller="HomeController">
<p> {{person.lname}}</p>
<p>First Name = <span ng-bind="person.fname"></span></p>
<p>Enter Details</p>
<p>First Name : <input type="text" ng-model="person.fname"/></p>
<p>Last Name : <input type="text" ng-model="person.lname"/></p>
<p>Middle Name : <input type="text" ng-model="person.mname"/></p>
</div>
</body>
<script src="angular.min.js"></script>
<script>
function HomeController($scope)
{
$scope.person={
fname:'Amit',
lname:'Kumar',
mname:'Verma'
}
}
</script>
</html>
As you can see i have used controller for binding the values.
as soon as i change the value of textbox , it reflect in p tag.
means it called the Homecontroller , but it doesn't calling that controller. i have checked by setting debug point in firbug. It should call the homeController.
As i am new , so it might be a simple question.
As stated by Yoshi, please read the official guide for a better start. They provide tutorials there and then you can start looking from other sources.
To answer your question, the controller HomeController is not actually being called each time a change has been done to its models. Controllers provide a way to group models and functions inside a scope. Inside the controller, you can add functions which can be triggered in your view.
Here is a fiddle that shows your controller with a model and a function inside.
// model
$scope.person = {
fname: 'Amit',
lname: 'Kumar',
mname: 'Verma'
}
// function
$scope.resetNames = function() {
console.log('I am called!');
$scope.person.fname = 'Amit';
$scope.person.lname = 'Kumar';
$scope.person.mname = 'Verma';
}
Good luck with your learning!
Please read Official AngularJS doc on controllers and how they work.
You have probably misunderstood how they work and thus expecting a different result. It is also advised to follow the Official doc guidelines and do the following:
var myApp = angular.module('myApp',[]);
myApp.controller('HomeController', ['$scope', function($scope) {
$scope.person={
fname:'Amit',
lname:'Kumar',
mname:'Verma'
}
}]);
Here's a working fiddle too:
http://jsfiddle.net/hpeinar/LxyL8e5k/
As of angular 1.3, global controller functions are no longer supported.
You should register your controllers in the following way:
app.controller('HomeController', HomeController);

Simple AngularJS example with a module and controller in jsFiddle

This probably a combo jsFiddle/Angular question, but I'm trying to learn me some basic Angular and I'm not sure why it only works when I include the controller JS in the HTML pane. jsFiddle here
Basically the following works:
<div ng-app="myAppModule" ng-controller="someController">
<!-- Show the name in the browser -->
<h1>Welcome {{ name }}</h1>
<p>made by : {{userName}}</p>
<!-- Bind the input to the name -->
<input ng-model="name" name="name" placeholder="Enter your name" />
</div>
<script>
var myApp = angular.module('myAppModule', []);
myApp.controller('someController', function($scope) {
// do some stuff here
$scope.userName = "skube";
});
</script>
But if I try to move the JS within the script tag to the JavaScript pane, it fails.
Take a look at this article for some info about jsFiddle with Angular
Also more examples on AngularJS Wiki that you could use:
https://github.com/angular/angular.js/wiki/JsFiddle-Examples

Categories