Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does AngularJS ng-view not work locally?

Tags:

I have been working for a few hours on getting my links to click through to different views with my AngularJS app.

However, I can only seem to get the functionality to work online on Plunker.

I've been trying to test the click-through functionality on my machine locally and ng-view does not seem to load. When I download my Plunker code that I know is correct because it is working on Plunker, ng-view seems to quit working once it's hosted locally.

I've also had similar issues with ng-include and directives I've defined as their own HTML tags.

Is there a reason these don't work locally on my computer? (And a way I can fix it for testing purposes?)

You can view some of the code I'm talking about on Plunker here, if you'd like.

like image 383
user2494584 Avatar asked Jun 09 '14 16:06

user2494584


People also ask

What can I use instead of NG repeat?

You can consider using transclusion inside a custom directive, to achieve the behavior you are looking for without using ng-repeat.

How does ng-hide work?

The ng-hide directive hides the HTML element if the expression evaluates to true. ng-hide is also a predefined CSS class in AngularJS, and sets the element's display to none .

How does ng include work?

The ng-include directive includes HTML from an external file. The included content will be included as childnodes of the specified element. The value of the ng-include attribute can also be an expression, returning a filename. By default, the included file must be located on the same domain as the document.


1 Answers

Both ng-view and ng-include use AJAX to load templates. The problem is that browser by default does not allow AJAX requests to files located on your local file system (for security reasons). Therefore you have two options:

  1. Run local web server that will serve your files
  2. Tell your browser to allow local files access

If you are on Mac, the fist option is rather easy since you have several built-in web servers (Apache httpd and Python module called SimpleHTTPServer). To run Python SimpleHTTPServer module just open console in the folder your files located at and run

python -m SimpleHTTPServer 3000 

then open your browser and type http://localhost:3000. That's it.

If you are on Windows, it's also possible. You can install for example Wamp and serve files from it.

Second option is possible with Chrome, just run it with --allow-file-access-from-files option from command line or add this flag to shortcut after path to Chrome executable.

This resource may also be useful to understand how to run things locally in different browsers and using different web servers.

like image 138
Vadim Avatar answered Oct 02 '22 09:10

Vadim