<body ng-init="user=${userID};month=${month};curPageNum=${currentPage}">
I wrote this by JSP,and i initial the value in the body tag,
but in the controller,i wrote:
console.debug($scope.user + " "+$scope.month}
but only $scope.month have the value,$scope.user displays undefined,
and after some tries, i found that if the value contains letters,it just display undefine,only the pure number can work.
I don't know why this happen,so can you help me to solve this?Thank you very much
Because your initialisation of the variables does not wrap the values in strings they are failing to assign. the JS is reading it as user=mary; i.e. assign the variable mary to user. That's why numbers work. Try this :
<body ng-init="user='${userID}';month=${month};curPageNum=${currentPage}">
You can refer Dynamic ng-init variable - Angularjs
Also, You can refer http://www.w3schools.com/angular/angular_directives.asp
you can initialise value as below
<body ng-init="user='${userID}';month='${month}';curPageNum='${currentPage}'">
Related
In html am rendering some value in the and passing it as a input to a component like
below
<test-component
[title]= "data?.Kevin?.title"
</test-component>
Please be noted data?.Kevin?.title will give the title name.How ever, in the above, I want to render the name a bit dynamically like below
<test-component
[title]= "data?.{{name}}?.title"
</test-component>
{{name}} should render kevin and the tile value needs to be passed to the component.
Here the name is not being rendered correctly.
Any help is highly appreciated.
the solution is pretty simple: [title]= "data?.[name]?.title"
the equivalent of the first example would be: [title]= "data?.["Kevin"]?.title"
read more on https://www.w3schools.com/js/js_objects.asp see "Accessing Object Properties"
How do I set a data attribute without adding a value in jQuery? I want this:
<body data-body>
I tried:
$('body').attr('data-body'); // this is a getter, not working
$('body').attr('data-body', null); // not adding anything
Everything else seems to add the second arguments as a string. Is it possible to just set an attribute without value?
The attr() function is also a setter function. You can just pass it an empty string.
$('body').attr('data-body','');
An empty string will simply create the attribute with no value.
<body data-body>
Reference - http://api.jquery.com/attr/#attr-attributeName-value
attr( attributeName , value )
Perhaps try:
var body = document.getElementsByTagName('body')[0];
body.setAttribute("data-body","");
The accepted answer doesn't create a name-only attribute anymore (as of September 2017).
You should use JQuery prop() method to create name-only attributes.
$(body).prop('data-body', true)
You can do it without jQuery!
Example:
document.querySelector('button').setAttribute('disabled', '');
<button>My disabled button!</button>
To set the value of a Boolean attribute, such as disabled, you can specify any value. An empty string or the name of the attribute are recommended values. All that matters is that if the attribute is present at all, regardless of its actual value, its value is considered to be true. The absence of the attribute means its value is false. By setting the value of the disabled attribute to the empty string (""), we are setting disabled to true, which results in the button being disabled.
From MDN Element.setAttribute()
Not sure if this is really beneficial or why I prefer this style but what I do (in vanilla js) is:
document.querySelector('#selector').toggleAttribute('data-something');
This will add the attribute in all lowercase without a value or remove it if it already exists on the element.
https://developer.mozilla.org/en-US/docs/Web/API/Element/toggleAttribute
simply try this, it will definately work....
document.querySelector("audio").setAttribute("autoplay", "");
this will showed like below code;-
<audio autoplay>
</audio>
if you wrote like,
$("audio").attr("autoplay", "");
then, this will showed like below code;-
<audio autoplay="autoplay">
</audio>
We have a lot of good answers here.
But, you have to see that inside Firefox it gives you data-body="" and inside Chrome it gives you data-body only.
I have a variable "widgetDataHash" (string) and script tag like this:
<script
type="text/javascript"
id="widget"
data-hash="{{widgetDataHash}}"
src="www.widgetsrc.com">
</script>
I want to insert value of widgetDataHash to data-hash attribute like that. But when it run, it just show a normal text {{widgetDataHash}} instead of value of widgetDataHash.
Any solution for this ?
I think you should write like this [attr.data-hash]="widgetDataHash" for angular 2+ and for Angularjs you should write like this
ng-attr-data-hash="widgetDataHash"
I should mention that I found this solution (for Angularjs solution) in a link Nobita answered that. For more information check Conditionally adding data-attribute in Angular directive template answers
I'm using Jquery to get a list of elements having a class "x".
html:
<p class="x">Some content</p>
<p class="x">Some content#2</p>
If we use Jquery to get both these html elements and do something with it- we use something like:
$(".x").text("changed text");
This will change the text of both the paragraphs. From $(".x") - How can we add a array - subscript notation like we can do with getElementsByclassName as follows:
document.getElementsByClassName("x")[0].innerHTML
I tried this
$(".x")[0].text("asasa")- it doesn't work gives a typeerror in javascript console. I also tried get API here -http://jsfiddle.net/probosckie/jnz825mp/ - and it doesnt work
the error is Uncaught TypeError: $(...).get(...).text is not a function
None of the solutions below WORK!
You can use the get() method for accessing an element from the array, for example:
$(".x").get(index).textContent = "changed text";
More info: https://api.jquery.com/jquery.get/
And for obtaining HTML (innerHTML) you call the .html() function:
// This is equal to document.getElementsByClassName("x")[0].innerHTML
$(".x").get(0).innerHTML;
If you want to set the HTML, then just provide your HTML code inside the function call like this .html('<h1>Hello, World!</h1>').
EDIT: .get() returns the DOM object not the jQuery wrapped element. Therefore .text() and .html() doesn't work. Unless you wrap it.
More options:
$(".x").get(0).innerHTML;
$($(".x").get(0)).html();
$(".x:first").html();
You can do it like this way:
$('.x:eq(0)').text('changed text');
or:
$('.x').eq(1).text('bbb');
both works well
sorry for my before answer..
The solution $(".x").get(index)... will first match all .x (which is bad performance). And then it will filter
If you have 1000 .x it will fill an 1000 items in the jQuery object (before filtered)
But
$(".x:first").text("changed text"); will do better because it won't yield all .x and then filter , but will do it at a first single step (without filling 1000 items)
I'm trying to display a JSON object nicely (this means on several lines with indentation) with Alex Gorbatchev plugin : http://alexgorbatchev.com/SyntaxHighlighter/
Unfortunately, it all displays on a single line.
I'm using the javascript brush.
I've created a code pen : http://codepen.io/hugsbrugs/pen/XJVjjP?editors=101
var json_object = {"hello":{"my_friend":"gérard", "my_dog":"billy"}};
$('#nice-json').html('<pre class="brush: javascript">' + JSON.stringify(json_object) + '</pre>');
SyntaxHighlighter.highlight();
Please don't give a list of other plugins since I know there is a bunch but I don't want to load additional plugins ... I'd like to achieve it with this plugin.
Thanks for your help
Try indenting the json with the stringify method.
JSON.stringify(json_object, undefined, 2);
You can use the optional third parameter of JSON.stringify(...) which is the space argument.
Change:
JSON.stringify(json_object)
to:
JSON.stringify(json_object, null, '\t')
Here is your codepen updated to show the result of the above modifications. The above modification causes your JSON to be pretty printed over multiple lines.