GithubHelp home page GithubHelp logo

tadhao / angular_on_rails Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 1.28 MB

Basic guide for creating application for Rails 6 with Angular 8

Ruby 69.30% JavaScript 9.85% CSS 1.48% TypeScript 7.38% HTML 11.99%
rails6 angular8 webpacker

angular_on_rails's Introduction

README

Steps to follow to create Rails 6 with Angular 8

  • Create new project by running,
    rails new angular_on_rails
    cd angular_on_rails
  • The frontend will be Angular so installing dependencies of webpacker:angular as,
    rails webpacker:install:angular
  • Create new controller as,
    rails g controller dashboard
  • Add Index method to dashboard_controller.rb
  • Add root page to routes.rb as
    root to: 'dashboard#index'
  • Under views folder, add folder with name dashboard and underneath that folder add index.html.erb file
  • Add following lines to index.html.erb
    <hello-angular></hello-angular>
    <%= javascript_pack_tag "hello_angular" %>
  • Start the server by running rails s
  • Cheers!

Steps to follow to create custom .html page for Angular 8

  • First we need .html loader for Angular
    yarn add html-loader
  • Add following line of code to enviroment.js
    const { environment } = require('@rails/webpacker')
    const typescript = require('./loaders/typescript')
    environment.loaders.append('html', {
    	test: /\.html$/,
    	use: [{
    		loader: 'html-loader',
    		options: {
    			minimize: true,
    			removeAttributeQuotes: false,
    			caseSensitive: true,
    			customAttrSurround: [
    					[/#/, /(?:)/],
    					[/\*/, /(?:)/],
    					[/\[?\(?/, /(?:)/]
    			],
    			customAttrAssign: [/\)?\]?=/]
    		}
    	}]
    })
    environment.loaders.prepend('typescript', typescript)
    module.exports = environment
  • Add html extension to webpacker.yml as /config/webpacker.yml
    extensions:
    	- .tsx
    	- .ts
    	- .mjs
    	- .js
    	- .sass
    	- .scss
    	- .css
    	- .module.sass
    	- .module.scss
    	- .module.css
    	- .png
    	- .svg
    	- .gif
    	- .jpeg
    	- .jpg
    	- .html
  • Create html.d.ts file as app/javascript/hello_angular/html.d.ts and add following line of code
    declare module "*.html" {
    	const content: string
    	export default content
    }
  • Now enviroment is set, so let's create app.component.html as app/javascript/hello_angular/app/app.component.html and add code in it as below
    <h1>Hello {{name}}</h1>
  • Call it in the module of app.module.ts as,
    	import { Component } from '@angular/core';
    	import templateString from './app.component.html'
    
    	@Component({
    		selector: 'hello-angular',
    		template: templateString
    	})
    	export class AppComponent {
    		name = 'Tushar!';
    	}
  • Cheers!
  • Simple tuitorial to follow is given in the webpacker git repo (https://github.com/rails/webpacker/blob/master/docs/typescript.md#html-templates-with-typescript-and-angular)

angular_on_rails's People

Contributors

tadhao avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

zoodigital7

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.