Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
We have created the div using ng-bind directive in angular js. We are not able to apply style property to that same div
<div ng-bind-html="test"></div>
in contrller file i defined test variable as
$scope.test="<div style="background:red;"></div>
We need to apply the style property. using CSS property i can able to do. But each time the background needs to be changed.
may be wat you want is this:
<div ng-style="{'background-color': bgColor}"></div>
in your controller
$scope.bgColor = "red";
I dont know what you want but the only thing wrong with your code is yo uneed ng-bind-html-unsafe ... assuming you are using a version of angular < 1.2.
ng-bind-html runs the code through a $sanitize service, which checks for unsafe code. In your example I and "style" will be stripped out. The unsafe version does not perform this check and as a result if you use it in certain situations where you dont have complete control over(e.g. WYSIWYG editors saving to your database and displaying the comments.) the data you can have punks running around doing malicious scripts.
Example fiddle: http://jsfiddle.net/pW7WY/
Supporting code.
<div ng-app>
<div ng-controller="Ctrl">
<div id="nick" ng-bind-html-unsafe="test"></div>
</div>
</div>
--js
function Ctrl($scope) {
$scope.test='<div id="child" style="background:red;"></div>'
}
--css
#nick{
width: 300px;
height: 300px;
}
#child{
width: 200px;
height: 200px;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is there any reason that this attribute tag a[h2]{color:orange;} wont work in css? I Cant seem to get the attribute tag to work for any elements.
a[h2] {
color:orange;
}
Means that all <a> tags that have an attribute called h2 will be coloured orange; i.e. <a h2="whatever">hello, world!</a>.
This is probably not what you want, both because this would be invalid HTML (h2 is not an attribute of <a>, and custom attributes are only allowed if they start with data-), but also because I am assuming that you want to target <h2> tags that are inside of your <a> tags.
To that, use the following code:
a h2 {
color:orange;
}
That will colour all <h2> tags inside of <a> tags orange.
Read more about CSS Selectors here.
Did you make a reference in the html page to your stylesheet?
<link rel="stylesheet" type="text/css" href="theme.css">
If this is done, check whether the element has an h2 attribute. Something like <a h2="value">
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
How would we implement something like "padding: 0 10px" for inline styling on a React element? It throws an error if I give the padding or margin properties the shortcut syntax, so I have to explicitly declare paddingTop, paddingRight, etc. I don't see anything on the React docs to address this, so I'm wondering if it's possible to use the shortcut in React?
It is possible you just probably have a typo or accidentally wrote it wrong. The syntax to apply an inline style follows this pattern.
{{property: 'value'}}
you can't add a semi colon in the value for a property.
inline styles are denoted as an object for react. and the syntax to apply a property or read something as javascript in the react render method is also denoted with curly braces.
So to apply that to your specific question, you would just do this.
<div style={{margin: '0px 10px'}} />
if you are using a style variable that is defined before the return of your render function you can use it like so.
const divStyles = {
margin: '0px 10px'
}
// ... in the render return
<div style={divStyles} />
Sample Fiddle with shortcut padding and margin used
This question already has answers here:
How to control Sass Variable with javascript
(9 answers)
Closed 6 years ago.
Basically I want to use background color for each element as per user choice, user choice will be stored on database. User should webpage according to it's color profile. So, I need a idea to get that value from angular and store its value on scss variable and render it.
SCSS is a preprocessor, when you use SCSS it actually gets compiled into CSS and that's what you use.
Perhaps the best solution is to use ngStyle in the section you need.. body, div, or anywhere you want..
Example
<div ng-style="userStyle">
...
</div>
In your JS
$scope.userInfo = { backgroundColor: 'red' };
$scope.userStyle = {'background-color': $scope.userInfo.backgroundColor};
Assuming you have a userInfo object, with backgroundColor property.
This is not possible, angular is javascript code and runs in the clients browser, sass is pre-compiled to css, any variables will have been converted into the full values before it is rendered in the browser.
You would need to get angular to make a call to your server, and have the server generate a CSS file dynamically based on the value provided from angular. (I suppose you could do this on the client also, I'm sure there is a javascript library somewhere that lets you compile sass)
Scss compiled to css before build, so angular part can't control scss. You can use gulp/grunt to edit scss variables. But in your case it not helping.
The simple sulotion for you is to use ng-style.
<div ng-style="{'background-color': scopeUserColor}"><div\>
This question already has answers here:
AngularJS : Why ng-bind is better than {{}} in angular?
(12 answers)
Closed 7 years ago.
In my html I use a layout like this:
<h2>{{translation.settings}}</h2>
As you might guess, I am using a multi language setup.
Now, for some reason I am required to bind the angularjs controllers later than normal and so for a brief moment {{translation.settings}} is rendered on my page.
Obviously this is not preferable.
I have been working with knockout for a while and one way to handle such a inconvenience is to set the element's value through a html tag, e.g.:
<h2 data-bind="text: translation.settings"></h2>
Is there a way in angularjs to set the inner element's text-value through a html-tag?
Or, since this is a possible XY problem, can I suppress the rendering of {{translation.settings}} in the <h2> element?
To prevent to show the template before it get a value use ng-cloack directive. You can use documentation here https://docs.angularjs.org/api/ng/directive/ngCloak
This question was asked on the Mailing List by Tami Wright...
I'm moving into the AngularJS world from JQuery and am not quite sure how to translate a particular use case which is a no brainer in JQuery. That use case is enabling/disabling or hiding/showing form elements based on the change of a select element in the same form. Any ideas/suggestions/pointers anyone would be willing to share to help me get this working?
And for an idea of what I'm trying to do here is some code to illustrate:
$('#ddl').change( function (e) {
var selectedValue = $(this).val();
switch(selectedValue){
case 1:
// Hide/show or enable/disable form elements here with Javascript or Jquery
// Sample enable code
document.getElementById("username").readOnly = false;
document.getElementById("username").style.background = "transparent";
document.getElementById("username").style.color = "#000000";
// Sample disable code
document.getElementById("first_name").readOnly = true;
document.getElementById("first_name").style.color = "#c0c0c0";
break;
}
return false;
});
Thank you in advance,
Tami Wright
One of the major design goals of AngularJS is to allow application developers to build web apps with little or no direct manipulation of the DOM. In many cases this also leads to a much more declarative style of programming. This allows business logic to be easily unit tested and greatly increases the rate at which you can develop applications.
Most people coming from a jQuery background have a bit of a hard time getting away from basing their development around DOM manipulation and in particular using the DOM as the domain model of their application.
AngularJS actually makes if very easy to change the appearance of input elements based on user events or data changes. Here is one example of how the problem above could be achieved.
Here is the working demo: http://plnkr.co/edit/zmMcan?p=preview
<html ng-app>
<head>
...
<style type="text/css">
.disabled {
color: #c0c0c0;
background: transparent;
}
</style>
</head>
<body ng-init="isEnabled=true">
<select ng-model="isEnabled" ng-options="choice == 'Enabled' as choice for choice in ['Enabled', 'Disabled']"></select><br>
User Name: <input ng-model="userName" ng-readonly="!isEnabled" ng-class="{ disabled: !isEnabled }"><br>
First Name: <input ng-model="firstName" ng-readonly="!isEnabled" ng-class="{ disabled: !isEnabled }">
</body>
You can see that instead of writing imperative code, we are able to declare that the class and the readonly attributes of the input are bound to the value of the select. This could be enhanced with complex computation of the values in a controller if you wished.
From AngularJS official documentation on directives :
Directives are a way to teach HTML new tricks. During DOM compilation directives are matched against the HTML and executed. This allows directives to register behavior, or transform the DOM.
Angular comes with a built in set of directives which are useful for building web applications but can be extended such that HTML can be turned into a declarative domain specific language (DSL).
In order to enable/disable input, the easiest way in AngularJS way is to use the following ngDisabled directive. For showing/hiding elements, use the following ngShow and ngHide directives
AngularJS has nice examples for all these .