Invoke function from div with angularJS parameter - javascript

Below I'm attempting to convert millisecond to javascript date :
<div ng-app>
<div ng-controller="TodoCtrl">
<div>
<script type="text/javascript">
parseDate({{test}})
</script>
</div>
</div>
</div>
function TodoCtrl($scope) {
$scope.test = "1429831430363"
}
function parseDate(date) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return months[date.getUTCMonth()] + ' ' + date.getUTCDate();
}
http://jsfiddle.net/U3pVM/15141/
But error Uncaught SyntaxError: Unexpected token { is thrown on console.
What is correct method of invoking function from div with angularJS parameter ?

use this code : see fiddle
<div ng-app>
<div ng-controller="TodoCtrl">
<div>
{{parseDate(test)}}
</div>
</div>
</div>
function TodoCtrl($scope) {
$scope.test = "1429831430363"
$scope.parseDate=function(date) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return months[(new Date()).getUTCMonth()] + ' ' + (new Date()).getUTCDate();
}
}

A more elegant solution to format a date is creating a custom filter because is more clean and you can re-use it everywhere:
Filter:
myApp.filter('myMillisecondsToUTCDate', [function() {
return function(milliseconds) {
var date = new Date(milliseconds);
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return months[date.getUTCMonth()] + ' ' + date.getUTCDate();
};
}]);
Controller:
myApp.controller('MyCtrl', function($scope, $filter) {
$scope.testDate = 1429831430363;
// Re-Use the filter everywhere
$scope.formattedDate = $filter('myMillisecondsToUTCDate')($scope.testDate);
});
Html:
<div ng-controller="MyCtrl">
Format a date using a custom filter: {{testDate | myMillisecondsToUTCDate}}<br/>
Formatted date {{formattedDate}}
</div>
Check the demo fiddle

You can use angular.element and jqLite.scope to get the scope object. Remember that angular and your app need to be initialized for this to work, so you'd better call parseDate in a callback.
parseDate(
angular
.element('[ng-controller="TodoCtrl"]') // but you'd rather give it an ID / class name / whatever
.scope()
.test
);

Modififying your code:
We dont need script inside divs as they are still evaluated seperately.
defined parseDate inside angularjs controller, If this is really general, define it as a service/factory and use it everywhere. That is the angular way.
Your test is a string. It doesnot have getUTCMonth function, it should be a number so that it can be converted to a date so that we can use the above function.
following is the modified code, which works:
<div ng-app>
<div ng-controller="TodoCtrl">
<div>
{{parseDate(test)}}
</div>
</div>
</div>
And the controller:
function TodoCtrl($scope) {
$scope.test = 1429831430363;
$scope.parseDate = function(date) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
date = new Date(date);
return months[date.getUTCMonth()] + ' ' + date.getUTCDate();
}
}
See this fiddle: http://jsfiddle.net/U3pVM/15146/

Related

Remove first and last grid lines in highcharts?

I want to separate the points on a line graph by vertical lines in Highcharts. I can do this with grid lines, but I would like the lines to display only between points and not before the first point and after the last one. In the fiddle there should be no grid lines before 'Jan' and after 'Dec'. Is it possible?
Here is my x-axis
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
gridLineWidth: 1,
minorGridLineWidth: 0
}
Just delete two last <path> elements in the group <g> element .
Example on fiddle
Axis has classname attribute.
set an axis classname.
set CSS to hide the last path element.
.axis-container {
path:last-child {
display: none;
}
}

Passing a string from .NET to a javascript variable

I have tried many ways to pass a string from .net to javascript including .NET serializer, and register startup scripts.
I am looking for the simplest way to populate a javascript variable with a string generated in code behind.
This is what I am currently trying but output is ""
I am open to other suggestions.
VB.NET
Public Property ServerVariable As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim ServerVariable = "'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'"
End Sub
JAVASCRIPT
var data = "<%= ServerVariable %>";
...
List : [data]
DESIRED RESULT
List : ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
This should be a comment, sorry, btw you can access to public props in the aspx;ascx, so this should help
vb.net
Public Property ServerVariable As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
ServerVariable = "['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']"
End Sub
JS
var data = "<%= ServerVariable %>";
You are doing it correctly, only thing is that you need to do .split(",") in javascript to get desired result.
Like var list = console.log(data.split(","));
Missing get and set values. Passed variable ServerVariable needs to have a value applied to it.
Private MyVar As String = ""
Public Property ServerVariable As String
Get
Return MyVar
End Get
Set(value As String)
MyVar = value
End Set
End Property
MyVar = "['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']"

AngularJS how can select value from ng-options

i am inserting value in select ng-options and i want to select in dropdown for particular month.
this is return all 12 month arrray, so how can i fetch only selected month from dropdown.
Script:
$scope.Month = ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
$scope.selectmonth = $scope.Month;
$scope.SaveReport = function () {
PrintReportRepository.save(
{
SelectMonth: $scope.selectmonth,
},
html
<select class="form-control" ng-model="selectmonth" ng-options ="months as months for months in Month">{{months}}</select>
For setting first item selected use $scope.selectmonth = $scope.Month[0], by setting $scope.selectmonth = $scope.Month U setting whole array as a model
use this code: see plunker
controller:
$scope.Month = ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
$scope.SaveReport = function () {
console.log('SelectMonth', $scope.selectmonth);
}
html:
<select class="form-control" ng-model="selectmonth" ng-options ="months as months for months in Month" ng-change="SaveReport()"></select>

How to create Jquery month select calender

I want a jquery month select calender. can anyone give me a website link or steps how to create month view calender like this image. please help me
Fiddle : Enjoy this : http://jsfiddle.net/abdennour/pyS99/
options = {
pattern: 'yyyy-mm', // Default is 'mm/yyyy' and separator char is not mandatory
selectedYear: 2010,
startYear: 2008,
finalYear: 2012,
monthNames: ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez']
};
$('#custom_widget').monthpicker(options);
Source :
http://lucianocosta.info/jquery.mtz.monthpicker/

Highcharts min y-axis value

Is it possible to set a min y-axis value when the series values are the same?
For example:
http://jsfiddle.net/6hyfk/1/
I would like to see 0.00019 as y-axis value.
If i change it to:
series: [{
data: [0.00019,0.00020,0.00019,0.00019]
}]
it looks a lot better.
So the answer largely depends on your data. If for example, you know your values are all positive, setting a yAxis min helps your data display a bit better. You know your data better than I, so I'd look at the yAxis settings and see what you can change to get a better result, but for example, this is a JSFiddle that sets a minimum value:
$(function () {
$('#container').highcharts({
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
tickLength: 20
},
yAxis: {
min: 0
},
series: [{
data: [0.00019,0.00019,0.00019,0.00019]
}]
});
});
http://jsfiddle.net/gTG2z/
You can manually set the min and max on the X axis.
yAxis:{
min: 0,
max: .001
},
http://jsfiddle.net/6hyfk/2/
If you know what range is in your series, you can set accordingly minRange, so Highcharts will render more ticks as expected. For example: http://jsfiddle.net/Fusher/6hyfk/3/ Good thing about minRange is that it will work also for another values like that: http://jsfiddle.net/Fusher/6hyfk/5/
Probably,you don't need to hardcode min,max values.Do calculation for min and max and set to y Axis as shown below,
$(function () {
yMin = Math.min.apply(Math,data); // Find Min
yMax = Math.max.apply(Math,data); //Find Max
$('#container').highcharts({
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
min: yMin,
max: yMax
},
series: [{
data: data //Array of values
}]
});
});

Categories