0

I'm a beginner in Angularjs and ng-bootstrap. I've created a sample Web site and added navigation for my site. It's working good, but I have some issue, I'm trying to display current date as 2017-12-18 this one is not displayed, I want to know how to added correctly to my site I'm following this Angularjs - display current date

app-navbar.component.html

       <div ng-app ng-controller="Ctrl">
          {{date | date:'yyyy-MM-dd'}}<br/>
        </div>

    <script>
  function Ctrl($scope)
   {
    $scope.date = new Date();
     }
    </script>

app-navbar.component.ts

import { Component, OnInit } from '@angular/core';

    @Component({
      selector: 'app-navbar',
      templateUrl: './app-navbar.component.html',
      styleUrls: ['./app-navbar.component.css']
    })
    export class AppNavbarComponent implements OnInit {

      constructor() { }

      ngOnInit() {
      }

    }
5
  • are you sure what you want to do? you just cannot add script tags in html in Angular. Commented Dec 18, 2017 at 10:12
  • There is a misunderstanding between Angular and AngularJS. Can you clarify which one you want to use? Judging by the use of TypeScript you probably want to use Angular 2/4/5. But the examples you are looking at are for AngularJS (1 - 1.6) Commented Dec 18, 2017 at 10:13
  • Sir , how t o added app-navbar to controller, Its should be fix, I Need to add controller, but i don't know wheres to added this controller Commented Dec 18, 2017 at 10:14
  • 1
    @core114 it's best for you to stick with JavaScript and avoid ES6 syntax for now Commented Dec 18, 2017 at 10:17
  • Sir thanks for the guide Commented Dec 18, 2017 at 10:18

3 Answers 3

1
import { Component, OnInit } from '@angular/core';

    @Component({
      selector: 'app-navbar',
      templateUrl: './app-navbar.component.html',
      styleUrls: ['./app-navbar.component.css']
    })
    export class AppNavbarComponent implements OnInit {
      public date = new Date();
      constructor() { }

      ngOnInit() {
      }

    }

try this. For format refer: https://loiane.com/2017/08/angular-tips-formatting-dates-with-a-custom-date-pipe-dd-mm-yyyy/

Sign up to request clarification or add additional context in comments.

1 Comment

welcome @core114, Happy Coding. Could you please accept my answer if it helped you.
1

Remove script tags from your template, and update your component like this:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-navbar',
  templateUrl: './app-navbar.component.html',
  styleUrls: ['./app-navbar.component.css']
})
export class AppNavbarComponent implements OnInit {
  public date = new Date()
  constructor() { }

  ngOnInit() {
  }

}

Comments

1

you mixed angularjs by angular2 your app.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-navbar',
  templateUrl: './app-navbar.component.html',
  styleUrls: ['./app-navbar.component.css']
})
export class AppNavbarComponent implements OnInit {
  constructor() { }
  date = new Date();
  ngOnInit() {
  }

}

your app-navbar.component.html

 <div ng-app ng-controller="Ctrl">
          {{date | date:'yyyy-MM-dd'}}<br/>
        </div>

1 Comment

Sir,all of solutions are work for me, Im vote up all of you

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.