As I work on creating a directive in AngularJs, I am facing an issue. The directive is supposed to receive a JSON object from the app controller and display its content accordingly.
To better illustrate my problem, I have created a simple demo which can be accessed at: http://jsfiddle.net/darkyndy/6StLm/
The current structure of my JSON object is as follows:
ctrlItemJson = {
myk: "something",
state: {
step: 1.0
}
};
However, when accessing state.step
in the directive-link, controller, and view, I am getting 1 instead of 1.0. This discrepancy has led me to question why this conversion is happening.
I have discovered that setting the value as a string '1.0'
resolves the issue. Nevertheless, it is crucial for me to maintain the exact format as received from the controller.
I have also tested this behavior in JavaScript by initializing a variable with var a = 1.0
and then logging it using console.log(a)
, which resulted in 1
.
In light of this issue, I plan to discuss with the API team about sending the values as strings...
Solution:
1. If you cannot control the value:
When dealing with decimals (zeroes), such as 1.0
or 2.00
, opt for using Strings over floats (e.g., '1.0'
, '2.00'
).
2. If you can control and determine precision (directive will set the precision):
Refer to responses to this query for further guidance. Example: http://jsfiddle.net/SE5s3/