<input type="button" class="button" value="click"/><div class="text_block">start</div>
In jquery this code is right for me:
$(".button").click(function() {
if($(this).val() == "click"){
$(this).val('clicked');
$(".text_block").html("stop");
}
else if($(this).val() == "clicked"){
$(this).val('click');
$(".text_block").html("start");
}
});
And how can I do this right using angular js?
AngularJs code sample
Use angular watch DEMO
var app = angular.module('my-app', [], function () {
})
app.controller('AppController', function ($scope) {
$scope.toggle = true;
$scope.$watch('toggle', function(){
$scope.toggleText = $scope.toggle ? 'Click' : 'Cliked';
$scope.divText = $scope.toggle ? 'Start' : 'Stop';
})
})
HTML code sample
<button ng-click="toggle = !toggle">{{toggleText}}</button>
<div class="box on" >{{divText}}</div>
You actually don't need a bit of javascript. You can do it in plain angular HTML code:
<input type="button" class="button" ng-model="toggle" ng-click="toggle = !toggle" value="{{toggle ? 'clicked' : 'click'}}" />
<div class="text_block">{{toggle ? 'Stop' : 'Start'}}</div>
Just create two local variable inside the scope and change their value according to the current value of clicked button.
Like :-
<body ng-controller="hello">
<input type="button" class="button" ng-click="click()" value="{{myvalue}}"/><div class="text_block">{{value}}</div>
<script>
var app=angular.module('app',[ ]);
app.controller("hello",function($scope){
$scope.value="STOP";//default value
$scope.myvalue="click";
$scope.click=function(){
if($scope.myvalue=="click"){
$scope.myvalue="clicked";
$scope.value="START";
}else{
$scope.myvalue="click";
$scope.value="STOP";
}
}
});
</script>
</body>
Here is the plunker:-
http://plnkr.co/edit/QsW1zzQF4BdslMwMFPxU?p=preview
live demo: http://plnkr.co/edit/zflkeo
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js#1.3.x" src="https://code.angularjs.org/1.3.10/angular.js" data-semver="1.3.10"></script>
</head>
<body ng-controller="MainCtrl">
<button type="button" ng-click="theAngularWay()">{{btnState}}</button>
<div class="text_block">{{textBlock}}</div>
</body>
<script>
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.btnState = 'clicked';
$scope.textBlock = 'start';
$scope.theAngularWay = function() {
if($scope.textBlock === 'start') {
$scope.btnState = 'clicked';
$scope.textBlock = 'stop';
} else {
$scope.btnState = 'click';
$scope.textBlock = 'start';
}
}
});
</script
</html>
see
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>
<body>
<div ng-app="" ng-controller="MyController">
<input type="button" class="button" value="{{Start}}" ng-click="showMessage()" /> <div class="text_block">{{message}}</div>
</div>
<script>
function MyController($scope) {
$scope.Start = "click";
$scope.message = "";
$scope.showMessage = function () {
if ($scope.Start == "click") {
$scope.Start = "clicked";
$scope.message = "stop";
}
else {
$scope.Start = "click";
$scope.message = "start";
}
}
}
</script>
</body>
</html>
Related
I'm new to JavaScript and Node.js.
I'm trying to get a button in my webpage to call a method in my controller when clicked, but so far I'm getting no response from my controller.
How do I call the .submitEntry() function from my button?
Here's the index.html file:
<!DOCTYPE html>
<meta charset="UTF-8">
<html ng-app='app'>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body>
<div ng-controller='GameController' class='container'>
<h1>Palindrome Game</h1>
<h2>Submit a new word!</h2>
<form role='form'>
<div class='form-group'>
<input ng-model="name" class='form-control' placeholder="Your name">
<input ng-model="word" class='form-control' placeholder="Word">
<button ng-click='submitEntry()' class='btn btn-default'>Submit word</button>
</div>
</form>
<h2>Top Scores</h2>
<ul class='list-group'>
<li ng-repeat="score in scores | orderBy:'-points'" class='list-group-item'>
<strong>{{score.name}}</strong> {{score.points}} points
</li>
</ul>
</div>
<script src='http://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.4/angular.js'></script>
<script src='ng/app.js'></script>
</body>
And here's the app.js file:
var app = angular.module('app', []);
const button = document.getElementById('myButton');
button.addEventListener('click', function(e)
{
console.log('button was clicked');
});
app.controller('GameController', function($scope, GameService)
{
console.log("Debug 0");
$scope.submitEntry = function()
{
if (typeof $scope.name === 'undefined' || typeof $scope.word === 'undefined')
{
return;
}
var entry = {
name: $scope.name,
word: $scope.word
};
GameService.submitEntry(entry).success(function(points)
{
$scope.word = undefined;
GameService.getScores().success(function(scores)
{
$scope.scores = scores;
});
});
};
GameService.getScores().success(function(scores)
{
$scope.scores = scores;
});
});
app.service('GameService', function($http)
{
this.getScores = function() {
return $http.get('/api/getScores');
};
this.submitEntry = function(entry) {
return $http.post('/api/submitEntry', entry);
};
});
Thank you very much! Any help that could point me in the right direction will be very appreciated.
Here's a simple button that displays an alert once clicked.
function Channels($scope) {
$scope.test = function() {
alert('button clicked!')
}
}
angular.module('app', [])
.controller('Channels', Channels);
angular.bootstrap(document.body, ['app']);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<body ng-controller="Channels">
<button ng-click="test()">
test
</button>
</body>
i am learning AngularJs, any help is much appreciated, trying to display the div content that is checked, if nothing checked the div with 'selected' class name should appear.
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<input type="checkbox" class="myCheckbox" ng-model="aCheck" ng-change="checkbox1Func()">A
<input type="checkbox" class="myCheckbox" ng-model="bCheck" ng-change="checkbox2Func()">B
<div ng-if="selected">Hello from div container</div>
<div ng-if="aCheck">Hello from checkbox1</div>
<div ng-if="bCheck">Hello from checkbox2</div>
</body>
</html>
And the script
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.selected=true;
$scope.checkbox1Func=function(a){
$scope.selected=false;
};
$scope.checkbox2Func=function(b){
$scope.selected=false;
};
if($scope.aCheck === false && $scope.bCheck===false){
$scope.selected=true;
}
// $('.myCheckbox').click(function() {
// $(this).siblings('input:checkbox').prop('checked', false);
// });
});
Your existing logic will work if you check the if condition
if($scope.aCheck === false && $scope.bCheck===false){
$scope.selected=true;
}
inside both the functions checkbox1Func and checkbox2Func.
Wrap the check inside a function and call it on the ng-change event,
$scope.valid = function() {
if ($scope.aCheck === false && $scope.bCheck === false) {
$scope.selected = true;
}
}
HTML
<input type="checkbox" class="myCheckbox" ng-model="aCheck" ng-change="checkbox1Func();valid();">A
<input type="checkbox" class="myCheckbox" ng-model="bCheck" ng-change="checkbox2Func();valid();">B
DEMO
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.selected = true;
$scope.checkbox1Func = function(a) {
$scope.selected = false;
};
$scope.checkbox2Func = function(b) {
$scope.selected = false;
};
$scope.valid = function() {
if ($scope.aCheck === false && $scope.bCheck === false) {
$scope.selected = true;
}
}
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js#1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<input type="checkbox" class="myCheckbox" ng-model="aCheck" ng-change="checkbox1Func();valid();">A
<input type="checkbox" class="myCheckbox" ng-model="bCheck" ng-change="checkbox2Func();valid();">B
<div ng-if="selected">Hello from div container</div>
<div ng-if="aCheck">Hello from checkbox1</div>
<div ng-if="bCheck">Hello from checkbox2</div>
</body>
</html>
when i am using $scope object to bind function and variable,and making changes accordingly in HTML the code is working fine ,but not with this object.
var app = angular.module("myApp", []);
app.controller("myCtrl", myCtrl);
function myCtrl()
{
console.log("registering app");
this.valueOne="0";
this.valueTwo="0";
this.result="0";
this.add=function () {
console.log("in add");
this.result=parseFloat(this.valueOne)+parseFloat(his.valueTwo);
valueOne="";
valueTwo="";
}
this.subtract=function () {
this.result=parseFloat(this.valueOne)-parseFloat(his.valueTwo)
valueOne="";
valueTwo="";
}
this.multiply=function() {
this.result=parseFloat(this.valueOne)*parseFloat(his.valueTwo)
valueOne="";
valueTwo="";
}
this.divide=function() {
this.result=parseFloat(this.valueOne)/parseFloat(his.valueTwo)
valueOne="";
valueTwo="";
}
}
<!DOCTYPE html>
<html lang="en-US">
<script src="angular.js"></script>
<script src="app.js"></script>
<body>
<div ng-app="myApp" >
<div ng-controller="myCtrl">
<p>Value 1 : <input type="text" ng-model="myCtrl.valueOne" placeholder="Value 1"></br>
Value 2 : <input type="text" ng-model="myCtrl.valueTwo" placeholder="Value 2"></br>
<button ng-click="myCtrl.add()">Add</button>
<button ng-click="myCtrl.subtract()">subtract</button>
<button ng-click="myCtrl.divide()">divide</button>
<button ng-click="myCtrl.multiply()">multiply</button>
</p>
<h1>Calculation Result {{myCtrl.result}}</h1>
</div>
</div>
</body>
</html>
And from where i am taking reference it is working fine there
var app = angular.module("calculatorApp", []);
app.controller("CalculatorCtrl", CalculatorCtrl);
function CalculatorCtrl() {
this.resultValue = 0;
this.buttonClicked = function(button) {
this.selectedOperation = button;
}
this.computeResult = function() {
var number1 = parseFloat(this.input1);
var number2 = parseFloat(this.input2);
if (this.selectedOperation === '+') {
this.resultValue = number1 + number2;
}
else if (this.selectedOperation === '-') {
this.resultValue = number1 - number2;
}
else if (this.selectedOperation === '*') {
this.resultValue = number1 * number2;
}
else if (this.selectedOperation === '/') {
this.resultValue = number1 / number2;
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app="calculatorApp">
<head>
<title>Calculator App</title>
<script src='angular.js'></script>
<script src='app.js'></script>
</head>
<body>
<h1>Calculator App</h1>
<div ng-controller="CalculatorCtrl as ctrl">
<input type="text" ng-model="ctrl.input1"></input>
<span ng-bind="ctrl.selectedOperation"></span>
<input type="text" ng-model="ctrl.input2"></input>
<button ng-click="ctrl.computeResult()">=</button>
<span ng-bind="ctrl.resultValue"></span>
<p>
Choose operation:
<button ng-click="ctrl.buttonClicked('+')">+</button>
<button ng-click="ctrl.buttonClicked('-')">-</button>
<button ng-click="ctrl.buttonClicked('*')">*</button>
<button ng-click="ctrl.buttonClicked('/')">/</button>
</p>
</div>
</body>
</html>
Not able to get what i am doing wrong .
What you missed is a tiny detail:
In working code:
ng-controller="CalculatorCtrl as ctrl" it had this.
Where as, your code:
ng-controller="myCtrl" has this. It means your HTML code didn't have a reference of your controller.
You can just change it to ng-controller="myCtrl as myCtrl" and it should work fine!
Also, your code snippet was having occurances of his instead of this in controller methods. Fixed those. And following is your working snippet:
var app = angular.module("myApp", []);
app.controller("myCtrl", myCtrl);
function myCtrl()
{
console.log("registering app");
this.valueOne="0";
this.valueTwo="0";
this.result="0";
this.add=function () {
console.log("in add");
this.result=parseFloat(this.valueOne)+parseFloat(this.valueTwo);
valueOne="";
valueTwo="";
}
this.subtract=function () {
this.result=parseFloat(this.valueOne)-parseFloat(this.valueTwo)
valueOne="";
valueTwo="";
}
this.multiply=function() {
this.result=parseFloat(this.valueOne)*parseFloat(this.valueTwo)
valueOne="";
valueTwo="";
}
this.divide=function() {
this.result=parseFloat(this.valueOne)/parseFloat(this.valueTwo)
valueOne="";
valueTwo="";
}
}
<!DOCTYPE html>
<html lang="en-US">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js">
</script>
<script src="app.js"></script>
<body>
<div ng-app="myApp" >
<div ng-controller="myCtrl as myCtrl">
<p>Value 1 : <input type="text" ng-model="myCtrl.valueOne" placeholder="Value 1"></br>
Value 2 : <input type="text" ng-model="myCtrl.valueTwo" placeholder="Value 2"></br>
<button ng-click="myCtrl.add()">Add</button>
<button ng-click="myCtrl.subtract()">subtract</button>
<button ng-click="myCtrl.divide()">divide</button>
<button ng-click="myCtrl.multiply()">multiply</button>
</p>
<h1>Calculation Result {{myCtrl.result}}</h1>
</div>
</div>
</body>
</html>
How can i append the data from textarea to a div by pressing enter key using anglurjs. in this code i am appending the data on alertbox but i want to append the data on particular section(div) as like in chat apps
HTML
<div ng-app="miniapp">
<div ng-controller="Ctrl">
<input ng-enter="DoWork()" ng-model="MyText" />
<div id="chatContent" ng-model="chatData"></div>
</div>
</div>
app.js
var $scope;
var app = angular.module('miniapp', []).filter('moment', function() {
return function(dateString, format) {
return moment(dateString).format(format);
};
});
app.directive('ngEnter', function () {
return function (scope, element, attrs) {
element.bind("keydown keypress", function (event) {
if(event.which === 13) {
scope.$apply(function (){
scope.$eval(attrs.ngEnter);
});
event.preventDefault();
}
});
};
});
function Ctrl($scope) {
$scope.DoWork = function(){
alert('Hello World! ' + $scope.MyText);
$scope.chatData = $scope.$scope.MyText;
};
}
Check This Example.This example is on jquery.you can take help and use same login in your code.
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
function abc(){
var a=$("#mytext").val();
$("#mydiv").append(a);
}
</script>
</head>
<body>
<textarea id="mytext" rows="" cols=""></textarea>
<button onclick="abc()">Submit</button>
<div id="mydiv"></div>
</body>
</html>
I am trying to have the web page as easy to use with the keyboard as with the mouse and need to be able to respond when a control receives and loses the focus. I've built a small plunk to illustrate this. I've used the event names from jquery as the documentation seemed to say that was the appropriate thing to do.
Tabbing through the screen to each button should show text saying which button has focus.
Here's the html
<!DOCTYPE html>
<html ng-app="app">
<head>
<title>Demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular-route.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body data-ng-controller="bbbb">
<input id="b1" type="button" value="button1">
<input id="b2" type="button" value="button2">
<input id="b3" type="button" value="button3">
<h2 ng-show="showb1">Button1 has focus</h2>
<h2 ng-show="showb2">Button2 has focus</h2>
<h2 ng-show="showb3">Button3 has focus</h2>
</body>
</html>
and the js
var app = angular.module('app', []);
var controllerId = 'bbbb';
app.controller('bbbb', ['$scope', function ($scope) {
$scope.showb1 = false;
$scope.showb2 = false;
$scope.showb3 = false;
var b1 = angular.element('#b1');
b1.on('focusin', function (event) {
$scope.showb1 = true;
});
b1.on('focusout', function (event) {
$scope.showb1 = false;
});
var b2 = angular.element('#b2');
b2.on('focusin', function (event) {
$scope.showb2 = true;
});
b2.on('focusout', function (event) {
$scope.showb2 = false;
});
var b3 = angular.element('#b3');
b3.on('focusin', function (event) {
$scope.showb3 = true;
});
b3.on('focusout', function (event) {
$scope.showb3 = false;
});
}
]);
Help greatly appreciated
Please see here: http://plnkr.co/edit/zOk0CJv0IdMb3GzvLxT5?p=preview
<!DOCTYPE html>
<html ng-app="app">
<head>
<title>Demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular-route.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body data-ng-controller="bbbb">
<input id="b1" type="button" value="button1" ng-focus="showb1 =true" ng-blur="showb1 =false">
<input id="b2" type="button" value="button2" ng-focus="showb2= true" ng-blur="showb2 =false">
<input id="b3" type="button" value="button3" ng-focus="showb3= true" ng-blur="showb3 =false">
<h2 ng-show="showb1">Button1 has focus</h2>
<h2 ng-show="showb2">Button2 has focus</h2>
<h2 ng-show="showb3">Button3 has focus</h2>
</body>
</html>