Knockout options bindings not working - javascript

Ok I am probably doing something very silly that is preventing me from getting this to work, but here goes anyway:
I am trying to learn how to work with knockout and am trying to build a select list with options defined as an observable array. Here is the code:
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" src="./knockout-3.0.0.js"></script>
<script type="text/javascript">
var viewModel = {
availableQuestions : ko.observableArray(['Who?', 'What?', 'When?'])
};
ko.applyBindings(viewModel);
</script>
<p>Questions to Ask: <select data-bind="options: availableQuestions"></select></p>
</body>
</html>
This is basically right out of one of their own examples but I cannot get it to work. The select list is not populated at all. I am using the latest version of Chrome (31.0.1650.57) and have looked in developer tools to see if there are issues. I have confirmed that everything is loading properly (ie: knockout is loading, the html is valid) still nothing. Am I missing something obvious?
here is the fiddle:
http://jsfiddle.net/janarde/r9pCK/
EDIT
Thanks to PW Kad It turned out to be that the DOM wasn't loaded before the binding:
EDIT
Thanks to Ek0nomik for pointing out the need to put knockout stuff after the markup.
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" src="./knockout-3.0.0.js"></script>
<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
<p>Questions to Ask: <select data-bind="options: availableQuestions"></select></p>
<script type="text/javascript">
var viewModel = {
availableQuestions : ko.observableArray(['Who?', 'What?', 'When?'])
};
ko.applyBindings(viewModel);
</script>
</body>
</html>

You need to make sure you call applyBindings. Here is a working jsFiddle: http://jsfiddle.net/b4wHQ/
HTML
<p>Questions to Ask: <select data-bind="options: availableQuestions"></select></p>
Javascript
var viewModel = {
availableQuestions : ko.observableArray(['Who?', 'What?', 'When?'])
};
ko.applyBindings(viewModel);

Are you sure that the DOM is loaded before you are trying to apply bindings?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="./knockout-3.0.0.js"></script>
</head>
<body>
<p>Questions to Ask: <select data-bind="options: availableQuestions"></select></p>
<script type="text/javascript">
$( document ).ready(function() {
var viewModel = {
availableQuestions : ko.observableArray(['Who?', 'What?', 'When?'])
};
ko.applyBindings(viewModel);
});
</script>
</body>
</html>

Related

how to find specific class in WP

I have a text that I would like to change ("Happy Clients" in the code below). I tried to find the text on the WP dashboard and in the template editor but with no luck, how can i locate the class so I'll be able to change the text?
<div class="_slider_shadow"></div>
</div><!-- /.carousel -->
<div class="count"><div class="count-info"><strong id="targetElem">0</strong><span class="count-description"> Happy Clients</span></div></div>
<script type="text/javascript">
EDIT
I found a file that contains the following code that is related to text that i want to change:
<div class="count"><div class="count-info"><strong id="targetElem">0</strong><span class="count-description"><?php the_field('text_for_count'); ?></span></div></div>
<script type="text/javascript">
var endVal = <?php the_field('count'); ?>;
var numAnim = new CountUp("targetElem", 0, endVal - 300, 0 , );
numAnim.start(function() {
numAnim.update(endVal);
});
</script>
You can install Query Monitor plugin. It has a section called Template which shows all template files that have been used to load the page. That should help you to find what you need.
Example output
Using jQuery you can do this :)
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$(".count-description").text("No Clients");
});
});
</script>
</head>
<body>
<button>Click me</button>
<div class="enigma_slider_shadow"></div>
</div><!-- /.carousel -->
<div class="count"><div class="count-info"><strong id="targetElem">0</strong><span class="count-description"> Happy Clients</span></div></div>
</body>
</html>

Angularjs: Object property value does not update

I know there are multiple questions related to my issue, but I still have problem fixing this. I have the following html and JavaScript code:
<!doctype html>
<html ng-app="Demo">
<head>
<meta charset="utf-8" />
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js">
</script>
</head>
<body ng-controller="AppController">
<input type="" name="" ng-model="docs[1].value">
{{m3.value}}
{{m4}}
<script type="text/javascript">
var app = angular.module('Demo', []);
app.controller(
"AppController",
function( $scope ) {
$scope.docs=[{value:"first doc"}, {value:"second doc"}];
$scope.m3=$scope.docs[1];
$scope.m4=$scope.docs[1].value;
}
);
</script>
</body>
</html>
When I type in the input, the m3.value gets updated but m4 does not! I can't figure out why this is happening. Any comment is appreciated.
Statement 1 :
$scope.m3=$scope.docs[1];
This statement storing the reference for model docs[1]
so, {{m3.value}} will get updated model value.
Statement 2 :
$scope.m4=$scope.docs[1].value;
This statement copying the actual primitive value.
so, {{m4}} still get old value
Ok so the way I solved it is to add a watcher to m3.value:
$scope.$watch('m3.value', function(){
console.log('Changing');
$scope.m4 = $scope.m3.value;
});
And now $scope.m4 updates.

Issue in using ng-repeat,ng-click in angular js

Iam newbie in Angular js,I have started working on few snippets in angular but didnt get answers while working with two snippets.I have been scratching my head to understand ,Any help is greatly appreciated.In the below code whenever I am pressing the button the value isnt getting changed
<html ng-app="secondmodule">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js"></script>
<script>
var demo = angular.module("secondmodule", []);
demo.controller("controlsample", function() {
var app = this;
app.myname = "xyz";
app.mysurname = "mno";
app.myaddress = "pqr";
app.changeme = function() {
app.myname = "abc";
};
});
</script>
</head>
<body ng-controller="controlsample as samp">
<h1>hey iam {{samp.myname}}</h1>
<h2>{{samp.myaddress}}</h2>
<button ng-click="samp.changeme">clickme</button>
</body>
</html>
In the below code I have used ng-repeat ,but no output is being generated
<html ng-app="mymodule">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js"></script>
<script>
angular.module("mymodule", []).controller("mycontrol", function() {
this.bikes = [{
id: 1,
name: 'a',
not: 3
}, {
id: 4,
name: 'b',
not: 6
}, {
id: 7,
name: 'c',
not: 9
}];
});
</script>
</head>
<body ng-controller="mycontrol as ctrl">
<h1 ng-repeat=bikedemo in ctrl.bikes>
<h2 ng-bind="bikedemo.id"></h2>
<h2>{{bikedemo.name}}</h2>
<h2>{{bikedemo.not}}</h2>
</h1>
</body>
</html>
Thanks for your help in advance,Any resources suggested in learning angular will be of great use to me
first one
<script>
var demo=angular.module("secondmodule",[]);
demo.controller("controlsample",function($scope)
{
$scope.myname="xyz";
$scope.mysurname="mno";
$scope.myaddress="pqr";
$scope.changeme=function()
{
$scope.myname="abc";
};
});
</script>
<body ng-app = "secondmodule" ng-controller="controlsample">
<h1 >hey iam {{myname}}</h1>
<h2>{{myaddress}}</h2>
<button ng-click="changeme()">clickme</button>
</body>
second one
<html >
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js"></script>
<script>
angular.module("mymodule",[]).controller("mycontrol",function($scope)
{
$scope.bikes=[{id:1,name:'a',not:3},
{id:4,name:'b',not:6},
{id:7,name:'c',not:9}
];
});
</script>
</head>
<body ng-app="mymodule" ng-controller="mycontrol">
<ul ng-repeat ="bikedemo in bikes">
<li>
<h2 ng-bind="bikedemo.id"></h2>
<h2>{{bikedemo.name}}</h2>
<h2>{{bikedemo.not}}</h2>
</li>
</ul>
</body>
</html>
1st you miss open and closing bracket when execute a function
<button ng-click="samp.changeme()">clickme</button>
2nd replace h1 with div or ul/li tag. you could not nested header tag.
<div ng-repeat="bikedemo in ctrl.bikes">
<h2 ng-bind="bikedemo.id"></h2>
<h2>{{bikedemo.name}}</h2>
<h2>{{bikedemo.not}}</h2>
</div>
For first :
<html ng-app="secondmodule">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js"></script>
<script>
var demo=angular.module("secondmodule",[]);
demo.controller("controlsample",function()
{
var app=this;
app.myname="xyz";
app.mysurname="mno";
app.myaddress="pqr";
app.changeme=function()
{
app.myname="abc";
};
});
</script>
</head>
<body ng-controller="controlsample as samp">
<h1 >hey iam {{samp.myname}}</h1>
<h2>{{samp.myaddress}}</h2>
<button ng-click="samp.changeme()">clickme</button>
</body>
You missed calling the function ie functionName(). Just add those brackets.
For the second one, try this :
<html ng-app="mymodule">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js"></script>
<script>
angular.module("mymodule",[]).controller("mycontrol",function()
{
this.bikes=[{id:1,name:'a',not:3},
{id:4,name:'b',not:6},
{id:7,name:'c',not:9}
];
});
</script>
</head>
<body ng-controller="mycontrol as ctrl">
<h1 ng-repeat =bikedemo in ctrl.bikes>
{{bikedemo.id"}}
{{bikedemo.name}}
{{bikedemo.not}}
</h1>
</body>
</html>
Regarding the resources to learn AngularJS you can start with,
Shaping up with Angular.js - Code School a great course for Angular.js fundamentals.
Then you can follow up with,
A Better Way to Learn AngularJS - Thinkster and courses from egghead.io and you can read some book too, this book Pro AngularJS by Adam Freeman is great.
The courses from Pluralsight are great too
Your code has some errors:
For 1st Code Snippet
You did not specified your ng-app
Calling the function in ng-click should contain parenthesis as if you're calling a function in javascript ng-click="samp.changeme()"
For 2nd Code Snippet
Header tags cannot be nested with other header tags
Your ng-repeat expression is not enclosed in double quotes

Underscore method _.template() doesn't compile my template

I'm getting started with backbone.js and i want to build a template, containing only a html button, with my model attributes. So I defined a template in my html page as below:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'/>
<title>Backbone test 5</title>
<script src="underscore.js"></script>
<script src="jquery.js"></script>
<script src="backbone.js"></script>
<script src="backbone_test5.js"></script>
</head>
<body>
<div id="here"></div>
<script type="text/template" id ="button_template">
<button type="button" id="my_button"><%= text %></button>
</script>
</body>
</html>
and I'm trying to build my template in my view:
var Bouton_View= Backbone.View.extend({
view_template: _.template( $('#button_template').html() ),
events:{
'click':'onClick'
},
initialize: function(){
this.$el=('#here');
},
render: function(){
this.$el.html(this.view_template(this.model.attributes));
return this;
},
onClick: function(){
var increment=0;
increment=this.Model.get("number_of_click")+1;
this.Model.set({"text":increment});
this.Model.set({"number_of_click":increment});
this.render();
}
});
but when I run the page in the browser this error message show up:
I'm pretty sure the js file is not wrong because I've try this with another html file and it worked. So what is wrong with my template? thanks in advance
Why so?
Spoiler: the underscore templating engine is fine in this case.
This example's problem is that the code which describes your Bouton_View, which probably lies here:
<script src="backbone_test5.js"></script>
is executed before the DOM parser reaches
<script type="text/template" id ="button_template">
<button type="button" id="my_button"><%= text %></button>
</script>
element. This can be described as
Load backbone_test5.js file and execute its contents
Execute _.template( $('#button_template').html() )
Search DOM for element with id="button_template"
None found
Execute .html() function on a non-existent $ element => this results in null
Execute _.template(null) => this gives you the error you mentioned
How to fix?
There are several ways to do that.
HTML-only. Re-order the code so that DOM elements exist when queried:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'/>
<title>Backbone test 5</title>
</head>
<body>
<div id="here"></div>
<script type="text/template" id ="button_template">
<button type="button" id="my_button"><%= text %></button>
</script>
<script src="underscore.js"></script>
<script src="jquery.js"></script>
<script src="backbone.js"></script>
<script src="backbone_test5.js"></script>
</body>
</html>
With some js-code. You can wrap the contents of the backbone_test5.js file with the $(...) so that it only executes once the DOM content is ready:
$(function () {
var Bouton_View = Backbone.View.extend({ /* ... */ });
});

Setting values for a drop down list in javascript

I have defined a drop down list in html as follows. I have appended the values for the dropdown from javascript. The value I am trying to set is "5". But it does not work. Can someone tell me another way?
<html>
<head>
<title>Title</title>
<script type="text/javascript" src="phonegap-1.4.1.js"></script>
<script type="text/javascript" src="JS/Util.js"></script>
<script type="text/javascript" src="JS/Language.js"></script>
<script type="text/javascript">
function dropdown(){
document.getElementById("numberlist").appendChild(5);
}
</script>
</head>
<body onload="dropdown();">
<div id="main3"></div>
<select id = "numberlist"><option>12</option><option>24</option><option>36</option></select>
</body>
</html>
Copying from the original question. Will this solve your issue?
function dropdown(){
var drp = document.getElementById("numberlist");
var optn = document.createElement("OPTION");
optn.text="3";
optn.value="3";
drp.add(optn);
}

Categories