how to pass a variable to the ternary condition in angularjs? - angularjs

I tried this:
{{experience.current_work?"Present":{{experience.date_end | date:'MMM yyyy'}}}}
But this is wrong in ternary condition.How do I solve it?

You're already within an expression (ie {{...}}) so you don't need to start a new one
{{experience.current_work ? "Present" : experience.date_end | date:'MMM yyyy'}}
or maybe this if you're worried about order of evaluation
{{experience.current_work ? "Present" : (experience.date_end | date:'MMM yyyy')}}

You can do it like few ways:
Call the function below when you need to check the condition:
$scope.CheckCurrentWork = function() {
if ($scope.experience.current_work) {
//Do as you want "Present"
} else {
//Do as you want
experience.date_end | date: 'MMM yyyy'
}
}
OR
{{experience.current_work ? "Present" : experience.date_end | date:'MMM yyyy'}}
OR
If you want to apply your condition in any directive
<div ng-click="experience.current_work ?'Present' : experience.date_end | date:'MMM yyyy'">

Related

How to filter by some elements in Object?

I use filter in ng-repeat:
<tr ng-repeat="item in filtered = (campaign.data.issues | filter:filters) | orderBy : 'created_at'">
Where filtersis object:
$scope.filters = {status : "active"};
I tried to use complicated object for sorting by each status:
$scope.filters = [{status : "active"}, {status : "paused"}, {status : "finished"}];
But it does not work
I know simple solution to use login negation:
$scope.filters = {status : !"active"};
But I dont know how to use negation object in filter

ng-repeat in angularJs avoid duplicate values

I have object like this
{
{
"assetId" : "560",
"assetName" : "Testname",
"message" : "hai",
"timestamp" : 1452585984181
},
{
"message" : "haii",
"timestamp" : 1452585999592
},
{
"assetId" : "560",
"assetName" : "Testname",
"message" : "heloo",
"timestamp" : 1452586221604
}
}
show to this object i am using ng-repeat. My question is
I need to show all message using ng-repat comes under single assetName. but in this object two objects have same assetName and assetId. i need to show the messages both same objects but no need to repeatably show the assetName in top.
How can i only avoid the duplicate assetName and assetId. I used
<div class="container" data-ng-repeat="data in dataList | unique:'assetId'">
But it's completely removing the object. I need the message from all objects.
is it possible.Please help
This is the out put i am expecting.:
I think you should create your own custom filter
yourApp.filter('customuniqueFilter', function() {
return function( array, propertyThatMustBeUnique) {
var newArray = [];
for (i = 0; i++; i < array.length){
if ( notYetInYourArray ){ // I was too lazy to think about a way to do it ;-) feel free to update my answer
newArray.push(array[i]);
}
}
return newArray;
}
});
and then use it like this :
<div class="container" data-ng-repeat="data in dataList | customunique:'assetId'">
How about this:
<div class="container" data-ng-repeat="data in dataList | unique:'assetId'">
<span>{{data.assetId}}</span>
<div data-ng-repeat="data2 in dataList | filter:(data.assetId === undefined ? {assetId:'!'} : data.assetId)">
<span>{{data2.message}}</span>
</div>
</div>

AngularJS date filter with condition

I use ng-bind and the date filter to output a time of a date.
<span ng-bind="ctrl.model.myDate | date:'HH:mm'"><span>
Now I would like to be able to switch the output between 12 and 24h format, with this filter: date:'HH:mm' and date:'hh:mm'
Therefore I have a property:
model.is24h= true
How can I insert a condition into the ng-bind expression to evaluate my property to output in 12h or 24h format?
Something like:
<span ng-bind="ctrl.model.myDate | {{ctrl.model.is24h: date:'HH:mm' || date:'hh:mm'}}"><span>
Just add a new filter with the variable as an argument
http://jsfiddle.net/HB7LU/13224/
HTML
<span ng-bind="myDate | newDate:is24h"></span>
<button type="button" ng-click="is24h = !is24h">Swap</button>
JS
myApp.filter('newDate', function ($filter) {
return function (input, arg) {
var hFormat = arg ? 'HH' : 'hh';
return $filter('date')(new Date(input), hFormat + '.mm');
};
});
Try to change your filter code for this condition.
angular.module('yourmodule').filter('date', function ($filter, $scope) {
return function (input) {
if (input == null) { return ""; }
if ($scope.is24h) {
return $filter('date')(new Date(input), 'HH:mm').toUpperCase();
}
return $filter('date')(new Date(input), 'hh:mm').toUpperCase();
};
});
html should be
<span ng-bind="ctrl.model.myDate | date"><span>
You can use trenary operator in directive parameter:
<span ng-bind="ctr.model.myDate | date:(ctrl.model.is24h?'HH':'hh')+':mm'"><span>

Date filter not working

Long story short, I'm trying to display a date that came from json.
This is what I tried:
{{ item.CreateDate | date }}
{{ item.CreateDate | date : 'MM/dd/yyyy' }}
This is what I get: /Date(1413010800000)/
I want to get 10/14/2014
What am I doing wrong? Thanks
Try
{{ item.CreateDate | msDate | date : 'MM/dd/yyyy' }}
And the filter
app.filter('msDate', function () {
return function (item) {
return new Date(parseInt(item.substr(6)));
};
});
Thanks #Brocco.

Ignore Time Zone Angularjs

Is there a better way to ignore an timezone in Angularjs:
"2014-01-18 14:30:00" Instead Of "2014-01-18 15:30:00"
function Scoper($scope) {
$scope.datum = "2014-01-18T14:30:00Z";
}
<div ng:app ng:controller="Scoper">
DateTime <br />
Angular: {{datum | date:'yyyy-MM-dd HH:mm:ss'}} <br />
</div>
http://jsfiddle.net/samibel/2rMXJ/
I was experimenting the same problem for a while. There is a timezone possible parameter to the date filter which I think should be the preferred solution, instead of making your own filter and append it. So, this is what worked for me:
{{ someAcceptedDateFormat | date : 'shortTime' : 'UTC' }}
I found this answer: Why does angular date filter adding 2 to hour?
Here is an example:
Just pipe another filter:
app.filter('utc', function(){
return function(val){
var date = new Date(val);
return new Date(date.getUTCFullYear(),
date.getUTCMonth(),
date.getUTCDate(),
date.getUTCHours(),
date.getUTCMinutes(),
date.getUTCSeconds());
};
});
In your template:
<span>{{ date | utc | date:'yyyy-MM-dd HH:mm:ss' }}</span>
I Have the solution:
app.filter('timezone', function(){
return function (val, offset) {
if (val != null && val.length > 16) {
return val.substring(0, 16)
}
return val;
};
});
template:
<span>{{ date | timezone | date:'yyyy-MM-dd HH:mm:ss' }}</span>
http://jsfiddle.net/samibel/n4CuQ/

Resources