Thursday, 15 March 2012

javascript - How to apply a function to an angularjs expression inside a controller? -



javascript - How to apply a function to an angularjs expression inside a controller? -

simple question, apply function within controller scope expression.

this html within controller

<p><span ng-if="paginadetail.pubdate !== '' ">vanaf {{paginadetail.pubdate}}</span></p>

this javascript

$scope.formatmysqltimestamptohumanreadabledatetime = function(sdatetime){ sdatetime = sdatetime.tostring(); var shumanreadabledatetime = sdatetime.substring(8, 10) + "/" + sdatetime.substring(5, 7) + "/" + sdatetime.substring(0, 4); shumanreadabledatetime += " " + sdatetime.substring(11, 13) + ":" + sdatetime.substring(14, 16); homecoming shumanreadabledatetime; };

and trying apply formatmysqltimestamptohumanreadabledatetime paginadetail.pubdate this

<p><span ng-if="paginadetail.pubdate !== '' ">vanaf {{formatmysqltimestamptohumanreadabledatetime(paginadetail.pubdate)}}</span></p>

or

<p><span ng-if="paginadetail.pubdate !== '' ">vanaf {{paginadetail.pubdate|formatmysqltimestamptohumanreadabledatetime}}</span></p>

but both ways not correct.

the first method works on console i've error

error: [$interpolate:interr] http://errors.angularjs.org/1.2.16/$interpolate/interr?p0=tot%20%7b%7bformatmysqltimestamptohumanreadabledatetime(paginadetail.endpubdate)%7d%7d&p1=typeerror%3a%20sdatetime%20is%20undefined t/<@http://mysite.local/js/libs/angular.min.js:6:443 g/r@http://mysite.local/js/libs/angular.min.js:78:354 yd/this.$gethttp://mysite.local/js/libs/angular.min.js:106:161 yd/this.$gethttp://mysite.local/js/libs/angular.min.js:109:285 f@http://mysite.local/js/libs/angular.min.js:71:234 f@http://mysite.local/js/libs/angular.min.js:75:408 ve/http://mysite.local/js/libs/angular.min.js:76:457

http://mysite.local/js/libs/angular.min.js line 89

and sec 1 doesn't work.

do have suggestions? lot.

if can offer alternative alternative controller based actions... looks great scenario filter

the thought utilize filter input while it's beingness set html {{mydate|format}}.

the filter defined like:

myapp.filter('format', function () { homecoming function (input) { //your date parser here }; });

or whatever else wish it. way can reuse without having set function in every controller wish utilize in.

here's fiddle can expand on, going.

edit looking more closely @ error think actual problem .pubdate not populated (typeerror: sdatetime not defined). seek set breakpoint in function or see console says.

javascript angularjs controller angularjs-scope

No comments:

Post a Comment