Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code - How to create a method from angular html template?

So, when I want to declare a new method in html of Angular component, WebStorm provides me opportunity to create this method in ts: Example webstorm

But, if I try same at VS code I can't create a method. Is there some solution? (extensions, maybe something in settings) Example vscode

like image 295
DromiX Avatar asked Jan 30 '21 18:01

DromiX


People also ask

How do I run an existing Angular Project in Visual Studio Code?

Now open Visual Studio Code. Go to "File" > "Open Folder" and select "First-Angular-Project" from the "Desktop". Now in the "TERMINAL" run ng new Angular-Project-Demo. Whenever it prompts with something like "Would you like to add Angular routing? (y/N)" press "y" and hit "ENTER".

What is a template in angular?

Get Data from a Server In Angular, a template is a chunk of HTML. Use special syntax within a template to build on many of Angular's features. Before learning template syntax, you should be familiar with the following: Each Angular template in your application is a section of HTML to include as a part of the page that the browser displays.

What is angular in Visual Studio Code?

Using Angular in Visual Studio Code Angular is a popular JavaScript library developed by Google for building web application user interfaces. The Visual Studio Code editor supports Angular IntelliSense and code navigation out of the box.

How to write HTML code inside a component in angular CLI?

Line 5-8: We can even write HTML code inside components using template property. Use backtick character (`) for multi-line strings. By default, Angular CLI uses the external template. It binds template with a component using templateUrl option.

What is a valid template syntax in angular?

Almost all HTML syntax is valid template syntax. However, because an Angular template is part of an overall webpage, and not the entire page, you don't need to include elements such as <html>, <body>, or <base>, and can focus exclusively on the part of the page you are developing.


Video Answer


2 Answers

You can use the extension My Code Actions v0.6.0.

Add this to your settings.json

"my-code-actions.actions": {
    "[html]": {
      "Create function {{atCursor:$1}}": {
        "file": "${fileBasenameNoExtension}.ts",
        "atCursor": "\\(click\\)=\"([^\"]+)\\(\\)\"",
        "text": "function {{atCursor:$1}}() {\n}\n\n"
      }
    }
  }

You can add more complex (multiple) edits if needed

You can have a look at the doc page of the extension to see an example of a complex edit to add / modify an import statement.

Adjust the languageID if Angular template files are not html.

like image 91
rioV8 Avatar answered Oct 31 '22 20:10

rioV8


This is now possible with the VSCode Angular Language Service plugin v14.2.0

  • feat: support fix the component missing member (angular/angular#46764)
  • feat: support code action (#1723)

vscode angular code action

like image 43
BuZZ-dEE Avatar answered Oct 31 '22 19:10

BuZZ-dEE