Cart Licenses Contact Support Online Creator Login/Register
Try
Buy
v 1.9.37
Try
Buy
v 1.9.37

Add Survey Creator / Form Builder to an Angular Application

This step-by-step tutorial will help you get started with the Survey Creator component in an Angular application. To add the component to your application, follow the steps below:

You can find the full code for this tutorial in the following GitHub repository: Get Started with Survey Creator / Form Builder - Angular.

If you are looking for a quick-start application that enables all Survey Creator features, refer to the following GitHub repository: Angular CLI + SurveyJS Quick Start Boilerplate.

Install the survey-creator-knockout npm Package

Survey Creator does not yet have a native implementation for Angular. However, you can integrate the version for Knockout into your Angular application. Run the following command to install the survey-creator-knockout package:

npm install survey-creator-knockout --save

Configure Styles

Open the angular.json file and reference Survey Creator and SurveyJS Library style sheets:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  // ...
  "projects": {
    "project-name": {
      "projectType": "application",
      // ...
      "architect": {
        "build": {
          // ...
          "options": {
            // ...
            "styles": [
              "src/styles.css",
              "node_modules/survey-core/defaultV2.min.css",
              "node_modules/survey-creator-core/survey-creator-core.min.css"
            ],
            // ...
          }
        }
      }
    }
  }
}

Note that although standalone surveys support multiple predefined themes, surveys within Survey Creator apply only one theme ("Default V2"). However, you can use the survey configuration (JSON object) produced by Survey Creator to build a standalone survey and apply any theme to it. You can also customize Survey Creator theme colors as shown in the following example:

View Customize Theme Using CSS Variables example

Configure Survey Creator

To configure the Survey Creator component, specify its properties in a configuration object. In this tutorial, the object enables the following properties:

  • showLogicTab
    Displays the Logic tab in the tab panel.

  • isAutoSave
    Automatically saves the survey definition JSON on every change.

const creatorOptions = {
    showLogicTab: true,
    isAutoSave: true
};

Pass the configuration object to the SurveyCreator constructor as shown in the code below to instantiate Survey Creator. Assign the produced instance to a constant that will be used later to render the component:

import { Component, OnInit } from '@angular/core';
import { SurveyCreator } from "survey-creator-knockout";

@Component({
  selector: 'survey-creator',
  templateUrl: './survey-creator.component.html',
  styleUrls: ['./survey-creator.component.css']
})
export class SurveyCreatorComponent implements OnInit {
  ngOnInit() {
    const creator = new SurveyCreator(creatorOptions);
  }
}
View full code
import { Component, OnInit } from "@angular/core";
import { SurveyCreator } from "survey-creator-knockout";

const creatorOptions = {
  showLogicTab: true,
  isAutoSave: true
};

@Component({
  selector: 'survey-creator',
  templateUrl: './survey-creator.component.html',
  styleUrls: ['./survey-creator.component.css']
})
export class SurveyCreatorComponent implements OnInit {
  ngOnInit() {
    const creator = new SurveyCreator(creatorOptions);
  }
}

Render Survey Creator

Switch to the component template. Add a page element that will serve as the Survey Creator container:

<div id="surveyCreator"></div>
#surveyCreator {
    height: 100vh;
    width: 100vw;
}

To render Survey Creator in the page element, call the render(containerId) method on the Survey Creator instance you created in the previous step:

// ...
@Component({
  // ...
})
export class SurveyCreatorComponent implements OnInit {
  ngOnInit() {
    // ...
    creator.render("surveyCreator");
  }
}
View full code
<div id="surveyCreator"></div>
import { Component, OnInit } from "@angular/core";
import { SurveyCreator } from "survey-creator-knockout";

const creatorOptions = {
  showLogicTab: true,
  isAutoSave: true
};

@Component({
  selector: 'survey-creator',
  templateUrl: './survey-creator.component.html',
  styleUrls: ['./survey-creator.component.css']
})
export class SurveyCreatorComponent implements OnInit {
  ngOnInit() {
    const creator = new SurveyCreator(creatorOptions);
    creator.render("surveyCreator");
  }
}
#surveyCreator {
    height: 100vh;
    width: 100vw;
}

Save and Load Survey Model Definitions

Survey Creator produces survey model definitions as JSON objects. You can persist these objects on your server: save updates and restore previously saved definitions. To save a JSON object, implement the saveSurveyFunc function. It accepts two arguments:

  • saveNo
    An incremental number of the current change. Since web services are asynchronous, you cannot guarantee that the service receives the changes in the same order as the client sends them. For example, change #11 may arrive to the server faster than change #10. In your web service code, update the storage only if you receive changes with a higher saveNo.

  • callback
    A callback function. Call it and pass saveNo as the first argument. Set the second argument to true or false based on whether the server applied or rejected the change.

The following code shows how to use the saveSurveyFunc function to save a survey model definition in a localStorage or in your web service:

// ...
@Component({
  // ...
})
export class SurveyCreatorComponent implements OnInit {
  ngOnInit() {
    // ...
    creator.saveSurveyFunc = (saveNo: number, callback: Function) => { 
      // If you use localStorage:
      window.localStorage.setItem("survey-json", creator.text);
      callback(saveNo, true);

      // If you use a web service:
      saveSurveyJson(
          "https://your-web-service.com/",
          creator.JSON,
          saveNo,
          callback
      );
    };
    // ...
  }
}

// If you use a web service:
function saveSurveyJson(url, json, saveNo, callback) {
  const request = new XMLHttpRequest();
  request.open('POST', url);
  request.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
  request.addEventListener('load', () => {
      callback(saveNo, true);
  });
  request.addEventListener('error', () => {
      callback(saveNo, false);
  });
  request.send(JSON.stringify(json));
}

To load a survey model definition JSON into Survey Creator, assign the definition to Survey Creator's JSON or text property. Use text if the JSON object is converted to a string; otherwise, use JSON. The following code takes a survey model definition from the localStorage. If the definition is not found (for example, when Survey Creator is launched for the first time), a default JSON is used:

const defaultJson = {
    pages: [{
        name: "Name",
        elements: [{
            name: "FirstName",
            title: "Enter your first name:",
            type: "text"
        }, {
            name: "LastName",
            title: "Enter your last name:",
            type: "text"
        }]
    }]
};

// ...
@Component({
  // ...
})
export class SurveyCreatorComponent implements OnInit {
  ngOnInit() {
    // ...
    creator.text = window.localStorage.getItem("survey-json") || JSON.stringify(defaultJson);
    // ...
  }
}

To view the application, run ng serve in a command line and open http://localhost:4200/ in your browser.

View full code
<div id="surveyCreator"></div>
import { Component, OnInit } from "@angular/core";
import { SurveyCreator } from "survey-creator-knockout";

const creatorOptions = {
  showLogicTab: true,
  isAutoSave: true
};

const defaultJson = {
  pages: [{
    name: "Name",
    elements: [{
      name: "FirstName",
      title: "Enter your first name:",
      type: "text"
    }, {
      name: "LastName",
      title: "Enter your last name:",
      type: "text"
    }]
  }]
};

@Component({
  selector: 'survey-creator',
  templateUrl: './survey-creator.component.html',
  styleUrls: ['./survey-creator.component.css']
})
export class SurveyCreatorComponent implements OnInit {
  ngOnInit() {
    const creator = new SurveyCreator(creatorOptions);
    creator.text = window.localStorage.getItem("survey-json") || JSON.stringify(defaultJson);

    creator.saveSurveyFunc = (saveNo: number, callback: Function) => { 
      window.localStorage.setItem("survey-json", creator.text);
      callback(saveNo, true);
      // saveSurveyJson(
      //     "https://your-web-service.com/",
      //     creator.JSON,
      //     saveNo,
      //     callback
      // );
    };
    creator.render("surveyCreator");
  }
}

// function saveSurveyJson(url: string | URL, json: object, saveNo: number, callback: Function) {
//   const request = new XMLHttpRequest();
//   request.open('POST', url);
//   request.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
//   request.addEventListener('load', () => {
//       callback(saveNo, true);
//   });
//   request.addEventListener('error', () => {
//       callback(saveNo, false);
//   });
//   request.send(JSON.stringify(json));
// }
#surveyCreator {
    height: 100vh;
    width: 100vw;
}

View full code on GitHub

Further Reading

Dismiss

Tell us what you think

Help us serve you better by taking this brief
survey. We are interested to learn more about
your experience of using our libraries.

We'd really appreciate your feedback.

Approximate time to complete: 2 min.

Start the survey