1

I am quite new to AngularJS I was wondering if is it possible to create new objects inside the parameter ng-bind?

I need this as I do not want to edit my model in JavaScript after receiving from signalr(Loop over all the string dates everywhere in the model and parse them to dates) this is what I am currently doing...

I would rather use ng-bind to help create it in the view such as:

ng-bind="new Date(date).toDateString();"

I know you can access properties of a object in angular ng-bind as well as use other operations such as accumulative operations but is it possible to create an object like above.

Are there any disadvantages of this? as I understand the performance should be the same, the same amount of object initialization are occurring and "if" the date changes a new Date object with the new value will be created and the old one should be disposed.

Cheers.

8
  • You cant do that... expressions are evaluated against that respective scope. Angular parser does not evaluate js, it only evaluates a valid angular expression And to me doing this way is a bad practice as well. These initializations are not meant to be on the view they should go to the controller. Also checkout datefilter, that may be of help Commented May 27, 2015 at 2:47
  • I would have thought it was bad practice to recreate a JSON model, that is standard in the company by transforming the standard model. By using a controller will it allow me to transform the string date to a date object usable by the view with out changing the model? If so then that is a solution... Commented May 27, 2015 at 2:59
  • Using the controller set up viewmodel to be bound to the view, if that means you want to convert string date to date object, i'd suggest to do so.. And format it either in the controller itself, or you could even use date filter on the view to format it. Commented May 27, 2015 at 3:01
  • Ill have a look at the filter you provided, It looks like it should be able to do what I want, I really want to do as least modifications to the model as possible and am trying to avoid a view model as all the data I need is already provided in the model just the date is a string not a date ;). Commented May 27, 2015 at 3:19
  • var workingModel = serverModel; Then modify the workingModel.date property to look however you would like it to look. Then bind $scope.viewModel = workingModel. Or on the view: ng-bind=viewModel.date | date: 'shortDate' Commented May 27, 2015 at 3:28

1 Answer 1

1

As an answer to my question you can indeed create a object inside a ng-bind using a filter thanks to PSL for the idea.

Bind:

ng-bind="date | dateFilter;"

Code:

angular.module('app', []).filter('dateFilter', function ($filter) {
    return function (input) {
        if (input == null) {
            return "";
        }
        return new Date(input);
    };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.