Git Product home page Git Product logo

angular-contentstack-service's Introduction

Angular Contentstack Service

The Angular Contentstack module is a Contentstack JavaScript SDK wrapper for Angular, which means that you can use all methods and queries of JavaScript SDK.

Note: This Angular Contentstack module is applicable for Angular 2 and above.

There are many Angular boilerplates available, but using angular-cli is an easier and faster option. Let’s understand how to use this.

Initial Setup

Run the following command to install angular-cli globally:

npm install -g @angular/cli

Next, you need to create an Angular service example (named contentstack-service-example), and build and serve it. To do this, use the following command lines:

ng new contentstack-service-example
cd contentstack-service-example
ng serve

This starts your basic angular-cli app.

Add and Configure Contentstack Angular Module

You need to start by installing the JavaScript SDK. Navigate to the root folder and run the following command:

npm install --save contentstack

Use the contentstack module from the above repo (in lib folder) and add it in your application’s src/modules/contentstack folder.

Next, you need to configure the module. Add the contentstack module along with the config into your main App module:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ContentstackModule} from '../modules/contentstack/contentstack.module';

import { AppComponent } from './app.component';

const Config = {
  'api_key':'blt12345789',
  'access_token': 'blt12345789',
  'environment': 'development'
 };
 

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    ContentstackModule.initializeApp(Config)
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

This adds the Contentstack Angular module in your App module along with the config required for retrieving data from Contentstack.

Now, you can use the Angular Contentstack service anywhere in the app.

Use the Service

You can use the Contentstack service as follows:

import { Component } from '@angular/core';
import { ContentstackService } from '../modules/contentstack/contentstack.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
    constructor(private cs: ContentstackService) {}
    title = 'angular-starter';
    entryText:any = {};
    getEntry() {
          this.cs.stack().ContentType('footer').Entry('blt2283c80248da490d').fetch()
          .then(entry => {
              this.entryText = entry.toJSON();
          }, err => {
          });

    }
    ngOnInit() {
      this.getEntry()
    }
}

Try out all methods and queries mentioned in the JavaScript SDK Query documentation.

Example

Check out the example covered in the example-app folder in the repo above to understand the service well.

angular-contentstack-service's People

Contributors

aravindbuilt avatar contentstack-admin avatar nandeesh-gajula avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

drumguy16

angular-contentstack-service's Issues

TypeError: Cannot set properties of undefined (setting 'Content-Type')

TypeError: Cannot set properties of undefined (setting 'Content-Type')
at eval (request.js:52)
at new ZoneAwarePromise (zone-evergreen.js:960)
at Request (request.js:28)
at callback (utils.js:297)
at eval (utils.js:390)
at new ZoneAwarePromise (zone-evergreen.js:960)
at Object.sendRequest (utils.js:389)
at Entry.fetch (entry.js:414)
at MoneyMindfulnessService.getEntry (money-mindfulness.service.ts:40)
at MoneyMindfulnessService.getMoneyMindfulnessCms (money-mindfulness.service.ts:28)

getEntry() { this.css.stack() .ContentType('money_mindfulness_quiz_page') .Entry('blt1b119af1c9b845c9') .fetch() .then(entry => { console.log(entry.toJSON()); }, err => { console.log(err); }) }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.