Visual Studio Code, AngularJs, & Intellisense

My primary goal is to get VSCode intellisense to work with AngularJs.

I’m working on a new application and I’m developing it with AngularJS (Angular 1). I’m also using VSCode (Visual Studio Code) as the IDE. My understanding is that VSCode should provide intellisense for JavaScript code, which it does. In the following image, you can see that VSCode provides intellisense for the variable “d”. VSCode is smart enough to see that this is a string and displays a list of string functions to select from.

image

Since I’m developing with AngularJS, I would like to have intellisense here also.  In the index.html page, I have reference to my AngularJs library, but in my JavaScript file intellisense is not working as expected.

image

To get intellisense working for AngularJS in VSCode.  The following steps need to occur

  1. Install node.js typings package globally
  2. Run typings and install AngularJS types
  3. Create a jsconfig.json file

Assumptions of reader

  • Have knowledge of node.js and npm

Tools and version

  • node.js 6.2.1
  • npm 3.9.5
  • Visual Studio Code 1.2
  • Windows

1. The Situation

I’m creating an AngularJS application.  I have two files index.html and module.js.  The index.html file references angular.js library and module.js. The module.js file is where the JavaScript and Angular 1.5 component will live.

image

image

By default, VSCode provides intellisense for JavaScript.  In the following image you will notice that the variable “d” is a string and that VSCode provides intellisense  based on the context of “d” being a string.

image

In module.js, when I enter “angular.” I expect that intellisense to display a list of AngularJS methods and properties, but as you can see in the image below, this does not occur.  My goal in this blog post is to have intellisense display AngularJS methods and properties.

image

2. Install typings package

The typings package needs to be installed.  Since this package will be used for this solution and others.  I want to install it globally.  First lets’ see what packages are installed currently on the computer.

To figure out what packages are installed globally, run the following command.

npm list -g –depth=0

This command will return all packages at a root depth and not include any children or dependencies of the base packages.

image

As we can see the typings package has not been install. Let’s install it globally.

npm install -g typings

SNAGHTML75b4fe

Now that we installed typings it should now be available to use.  Notice the version of typing is 1.3.0.

image

3. Install AngularJS types

When I initially attempted to install angular typings, I received the following error (see image). This worked just a few days ago. But I install typings 1.3.0. and my typing install didn’t use the same format as pre typings 1.0 used.  You can read more about that here Updating Typings From 0.x to 1.0?

image

To install angular typings, I had to enter the following command:

typings install dt~angular –save –global

image

When the angular typing is installed the typings.json file and a folder called typings should be created.  See image below for more information .

image

4. Create a jsconfig.json file

Now create an empty jsconfig.json file.

image

If we were using TypeScript and transpiling code, then this file would contain configuration. But since we are only need intellisense to work, this file can remain empty

5. Validate if Intellisense is working for Angular

Go back to the module.js file. Enter “angular.”.  The intellisense should now display AngularJS methods and properties.

image

Resources

Understanding AngularJS – Simple Example

I did a similar post as this post for Backbone.js Understanding Backbone.js – Simple Example. Based on readers’ comments the backbone.js post seemed to assist many people. My goal with this post is to help others understand the core parts of AngularJS and how they work together.

The AngularJS application that we are building will display a list of movies and will have the ability to add movies. We could create this application with very minimum code, but extensibility and maintainability will be limited. My primary objective is to show how to structure an AngularJS application that provides a base of knowledge to create other AngularJS applications extensible and maintainable.

My target audience will have some knowledge about AngularJS, but are having a difficult time implementing all but the most basic applications. By simple, I mean you should only have to focus on AngularJS. We will not be using jQuery, Twitter Bootstrap, or other 3rd party libraries.

We could create this application with just a controller and a view. But in this post I will describe the following core AngularJS features:

  • Routes
  • Modules
  • Views
  • Controllers
  • Services

Above I mentioned I will keep this post simple, but we do need a web server. The AngularJS Theater application will load views and data (json files) from the server. I would like to do these examples by access “file:///E:/TempProjects/Theater/index.html#/” on local computer, but most browsers cannot access these files on local computer do to Cross Origin issues. Here a StackOverflow question and answer that describes this issue: Cross origin requests are only supported for HTTP but it’s not cross-domain. So we need a web server. Any web server will do, such as IIS, Node.js and Express or other server. In this example I will be using Node.js and Express. You will not need to know anything about Node.js; all that is need is for you to install Node.js and to execute a script that I will provide. I will step you through the complete process to get Node.js and Express up and running.

Google Chome will be the browser I use, but any current browser should work fine. The reason I pick Chome is for its development tools. I will be developing in JetBrains WebStorm IDE, but any IDE or text editor should be fine.

This post will be based on a contrived idea of displaying a list of movies at a theater. There is nothing fancy here.

The complete solution can be downloaded from here:
Source – Understand-AngularJS-Simple Example(Theater)

Based on comments from Hasan this example works with AngularJS 1.1.5. It does not work with AngularJS 1.2.3.

Continue reading “Understanding AngularJS – Simple Example”

How I Navigated to AngularJS

This article is about the process I’ve gone through of selecting AngularjS as my MV* JavaScript framework of choice. I will discuss the evolution I went through from learning jQuery, Knockout, Backbone.js, and eventually settling on AngularJS.

I’ve been programming in JavaScript since the late 90′s. JavaScript is not going anywhere. JavaScript is very popular today, and I believe it will only get more popular. For programming a browser, JavaScript is the least common denominator. This is the only language that all Browsers support. But, JavaScript has many pitfalls; just read the book JavaScript: The Good Parts by Douglas Crockford. In the early years, I used JavaScript mostly for validation and simple DOM manipulation. Writing different code for each browser sucked. Back in the early years JavaScript seemed like a toy, there weren’t many best practices and the expectations of what could be accomplished with JavaScript was limited.
Continue reading “How I Navigated to AngularJS”

Setting-up AngularJS, Angular Seed, Node.js and Karma

I’ve used AngularJS for a few months, but I have no knowledge when it comes to testing AngularJS apps. I have a subscription to PluralSight.com and wanted to go through their online video training course for AngularJS. Specifically with this course I want to learn how to use Karma to do testing.

I’m usually extremely happy with PluralSight.com course, but in the beginning of this course I was somewhat disappointed. In Section 7 (“Angular Seed”) new technologies were introduced. The author introduced Angular-Seed, Node.js and Karma. I’ve worked with Node.js, but there are probably many people who have never used it. I believe the author took for granted that the student knew Node.js. For those who have never used Node.js this could be an obstacle.

When I started the course I didn’t have Node.js installed. I installed Node.js and things didn’t work as intended. I couldn’t run tests in Node.js because Karma was not installed. Once I installed Karma, Chrome wouldn’t launch in the tests.

With all the issues I was having, I wondered if others were having the same problems. If others were having similar issues, were they discouraged and not continuing with the course. So I decided to create this blog post to help others to get stared with AngularJS, Angular-Seed, Node.js and Karma.

Here are my assumptions:

  • You are a windows developer
  • Google Chrome browser is installed
  • You will use the Windows Command Prompt instead of Bash
  • You have little or no knowledge of Node.js
  • You have little or no knowledge of Karma
  • You will use Node.js as the web server. I will step you through this.
  • You have access to and IDE. Visual Studio will be fine. NotePad++ would also probably work. I use JetBrains WebStorm. JetBrains has a free 30 day trial for WebStorm.

Here are the high level step we will follow:

  1. Download and install Angular-Seed
  2. Download and install JetBrains WebStorm (Optional)
  3. Download and install Node.js
  4. Confirm Node.js is installed
  5. Run Karma Unit Tests – will fail because Karma is not installed
  6. Install Karma
  7. Run Karma Unit Tests again – Will fail because Chrome will not start
  8. Add System Variable to Windows
  9. Confirm System Variable Were Added
  10. Run Unit Tests again – should succeed
  11. Confirm that Units are being tracked by Karma
  12. Start Web Server by using Node.js

Continue reading “Setting-up AngularJS, Angular Seed, Node.js and Karma”