AngularJS intellisense not working on Visual Studi

2019-01-07 09:55发布

问题:

According to this post intellisense should also be working on the new VS 2015, but so far I only get intellisense for the angular object and not for the dependencies or my custom modules.

Here's what I did:

  1. Added the angular.intellisense.js to the global javascript references at C:\Program Files (x86)\Microsoft Visual Studio 14.0\JavaScript\References
  2. Restarted VS2015 And then nothing, it just showed exclamation marks whenever I tried to use intellisense on a $http object.

I also added the file to the same place as my angular.js but it still didn't work. The question that I have in this case is, where should I place the file? on the angular public folder with only my angular.js, or on my dev angular folder where all the files downloaded from bower are.

I also tried addind it directly into the tools/options/text editor/javascriptr/intellisense/reference menu, on the Implicit(Web) reference group, but it still didn't work.

On my project I have the following folder structure inside the src folder:

  • wwwroot
    • app (my angular site stuff)
      • controllers
      • services
      • views
    • lib (js dependencies, only the .min.js file of each library)
      • angular
      • angular-route
      • ....
    • _references.js (the visual studio js references file, contains reference to the files inside the app and lib folders)
  • Libraries (contains the full libraries as downloaded by bower)
    • angular
    • angular-route
    • ...

As a side note, I don't have a /scripts folder and therefore no /scripts/_references.js file

.

回答1:

This was not working for me in Visual Studio 2015 RTM in a web project, but I solved the problem.

This project was not created with Visual Studio and does not have a _references.js file anywhere. So I think this will work in any situation.

I removed all other intellisense resources from within the VS UI to make sure what I did was what fixed it.

  1. Go to https://www.angularjs.org and pull up the download dialog box.

  1. Copy the Uncompressed CDN url. Today that happens to be https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js
  2. In Visual Studio 2015 RTM, go to Tools, Options, Text Editor, Javascript, Intellisense, References. Choose the appropriate Reference Group; for most web project this is Implicit (Web). Paste the url at the bottom text box and click the Add button. Don't dismiss the dialog box yet.
  3. Under Text Editor, Javascript, Intellisense, General, make sure the check box is checked for Download remote references.
  4. Click the OK button.
  5. (optional) If you want intellisense for the angular providers that you create (not part of the angular framework), add _references.js to the root of your project. Don't bother making a Scripts folder. Right click on it and choose auto-sync, then choose update. Go into it and remove any js files created by a build process. If you don't, they can be so large they will break intellisense. Be prepared for a ~5-10 second delay the first time you use intellisense, as it has to load all these references from your project.
  6. You may need to disable intellisense in Resharper for javascript if it interferes with the native intellisense.
  7. Restart Visual Studio. It will not work until you do this. Also, I'm paranoid about closing all other instances other than this instance first, so these settings "stick". So I suggest you do that before restarting this instance.


回答2:

As @Balthasar pointed out (and in case you are using Resharper) you will need to enable intellisense from Visual Studio for it to work:

Resharper -> options -> environment -> intellisense -> general, select 'Custom Intellisense' and for Javascript you can select Visual studio. Alternatively you can use the 'Visual Studio' statement completion (second option)



回答3:

i've just realized that the automatic order that _reference.js file uses (first my files then the framework's files) prevented intellinsense to work on other files that weren't the app.js file

this is how it now my _references.js looks like:

/// <autosync enabled="false" />
/// <reference path="angular.js" />
/// <reference path="angular-resource.js" />
/// <reference path="angular-ui-router.min.js" />
/// <reference path="jquery-2.1.4.js" />
/// <reference path="materialize/materialize.js" />
/// <reference path="../App/App.js" />
/// <reference path="../App/Controllers/productsController.js" />
/// <reference path="../App/Controllers/productsEditController.js" />
/// <reference path="../App/Controllers/valuesController.js" />
/// <reference path="../common/common.services.js" />
/// <reference path="../common/productsResource.js" />
/// <reference path="../common/valuesResource.js" />


回答4:

I had a similar issue and it turned out Resharper was blocking all the nice JavaScript intellisense I had setup in my _references.js file.



回答5:

Visual Studio intellisense for AngularJS is extremely sentimental (a nice way of saying that it's most likely poorly developed).

So, even if everything is well configured with the _references.js file, and you get intellisense for other libraries like jQUERY, you will most probably not get it for AngularJS.

For example, in VS 2015 community, the below directive will not show intellisense for the $http angular object, although everything works fine with the code:

In the image below, I add an empty array to the module (which means that the module will get created, and if another with the same name exists, it get overwritten), and intellisense starts working:

Here is a snippet of the code for you to test yourself (try and add the empty array here):

(function () {
    'use strict';

    angular
        .module('intellisence.sucks.directives')
        .directive('footer', footer);

    footer.$inject = ['$http'];

    function footer($http) {

        $http.
        var directive = {
            link: link,
            restrict: 'EA'
        };
        return directive;

        function link(scope, element, attrs) {
        }
    }

})();


PS: If you click on any folder on you project in the Solution Explorer and press Ctrl+Alt+A to add a new item, you get suggestion to add a AngularJs Directive, and If you do it, the Directive will be like the one I just showed you, without the empty array on the module declaration, so intellisence won't work with it. It won't work with the example that Microsoft gives to the users... Only works on the module creation, your first file and doesn't work from now on.



回答6:

I had the same issue. When I added angular, VS 2015 (RTM) modified my _references.js; basically it removed some of my lines in the file. When I added a reference for angula in my _references.js as below

/// <reference path="lib/angular/angular.js" />

I got my intellisense for Angular 1.4.3 in VS 2015!



回答7:

Follow this article to add more comprehensive intellisense to VS.

http://blogs.msdn.com/b/visualstudio/archive/2015/02/05/using-angularjs-in-visual-studio-2013.aspx

Download the angular.intellisense.js file and place it in the Program Files (x86)\Microsoft Visual Studio 12.0\JavaScript\References folder



回答8:

i had this issue.

The visual studio IDE will scan any js files referenced in the HTML files and use them if neccesary (for the intellisense etc).

My problem was i was referencing Angularjs using the CDN/http:// addresses and thus Visual studio did not have any information on angular.

Try downloading the angularjs js file directly and include in your project and reference in your index.html, rebuild (just in case) and try again and hopefully it should work.

Hope i helped.



回答9:

I have this issue for RTM ..apart from angular object and in a hello world complexity senario ($http and all custom objects dont work) .. Followed the _references setups . My only suspicsion is I upgraded from RC to RTM (no fresh install) have created a issue on Git [link]github.com/jmbledsoe/angularjs-visualstudio-intellisense/issues/… ..Grabbing a VHD Win 10 image and going to try that once the d/l finishes :( –

UPDATE

Ok with a bit of help here is teh answer .... The Intellisense engine never sees the module named "sportsStore" being created, since it is created in the app.html file in a script block:

<script>
  angular.module("sportsStore", ["customFilters","cart"]);
</script>

The VS Intellisense engine is "executing" the code in your project in order to discover how to do code completion. It's using the _references.js file as its starting point and executing each reference from that file, in order. Since VS Intellisense never executes a line of code that creates the AngularJS module named "sportsStore", it doesn't know how to do code completion.

Move the JavaScript code above into its own JS file (let's call it "app.js") and include a reference to it in _references.js. Make sure the "app.js" reference is immediately after the "angular.js" reference, since you need it to create the module before the other script files configure it. :D



回答10:

Just create an _references.js file in a folder named 'Scripts' (naming convention) at the root of your project. Update it with your necessary javascript files and you should be able to get intellisense. Here's a link on why such a thing was needed: http://madskristensen.net/post/the-story-behind-_referencesjs