0

I am new in angular and working in angular 5 (typescript) project and i need a JSON validator function, i got a function from internet but it is in purely JavaScript.

I want that function in my component, it actually checks that given JSON is in correct format or not.

<textarea ng-change="updateJsonObject()" ng-model="script" rows="10" 
 class="form-control" style="width: 100%" ng-style="formatting"> 

    var app = angular.module("app", []);

app.controller("mainController", function($scope) {
  $scope.formatting = {color: 'green', 'background-color':'#d0e9c6'};
  $scope.message = {
                BasketCost: '5.00',
                Fruit: ['Apple', 'Orange', {Name: 'Pear', Expires: '15/05/17'}],
  };

  $scope.isValid = true;

  $scope.script = JSON.stringify($scope.message);

  $scope.updateJsonObject = function() {
    try {
      JSON.parse($scope.script);
    } catch (e) {
      $scope.isValid = false;
      $scope.formatting = {color: 'red', 'background-color':'#f2dede'};
    }

    $scope.message = JSON.parse($scope.script);
    $scope.isValid = true;
    $scope.formatting = {color: 'green', 'background-color':'#d0e9c6'};
  };
});

what is the way to add this code in my component.ts,

1
  • That should be a fairly easy task, it's converting AngularJs code to Angular. Cf. angular.io/guide/upgrade Commented Dec 31, 2018 at 12:49

1 Answer 1

2

You can follow url https://stackblitz.com/edit/angular-8jjx4b

ngModelChange you need to use ngModelChange for reflecting the changes immediately.

<textarea (change)="updateJsonObject()" (ngModelChange)="updateJsonObject()" [(ngModel)]="script" rows="10" 
 class="form-control" style="width: 100%" [ngStyle]="formatting"></textarea>    

// in class

  message = {
    BasketCost: '5.00',
    Fruit: ['Apple', 'Orange', { Name: 'Pear', Expires: '15/05/17' }],
  };

  isValid = true;

  // script = '{' + JSON.stringify(this.message) + ')'; // testing
  script = JSON.stringify(this.message) ;


  formatting = { color: 'green', 'background-color': '#d0e9c6' };


  ngOnInit() {
    this.updateJsonObject();
  }

  updateJsonObject() {
    try {
      JSON.parse(this.script);
      this.formatting = {color: 'green', 'background-color':'#d0e9c6'};
      console.log('invalid');
    } catch (e) {
      console.log('invalid');
      this.isValid = false;
      this.formatting = {color: 'red', 'background-color':'#f2dede'};
    }
  }
Sign up to request clarification or add additional context in comments.

5 Comments

It works fine but there is a little issue, when I lose the focus i mean when the field is untouched then the error occurs if any
yes..that case we are handling inside ngOnInit. Or if you expecting any api call to resolve its better to call updateJsonObject inside the resposne block
its already there. ngOnInit() { this.updateJsonObject(); }
Yea, thats why we are suing ngmodelchange
ok i got it, just need this modifications (ngModelChange)="updateJsonObject($event)" and not need for (change) event

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.