ANGULARJS Study Notes (iii) single page webapp and routing (ng-route)
Source: Internet
Author: User
<span id="Label3"></p><p><p>As far as I know, routing ($route) this thing (perhaps plus $location) can be said to be the most important thing about Ng. Because angular at present the most important role is to do single page webapp, and routing this thing is able to do page jump Key.</p></p>1. Single page WebApp<p><p>Why call single page webapp? Because it's a single page .... The amount ... The point is that many of the webapp we see are multi-page, and can be skipped from one page to Another. well, it looks like this, but actually these pages are actually a page, because they are loaded in one time, as to why there is a jump effect? well, kinda like tab, uh. Its implementation principle before HTML5 is through Hashbang,h5 is History. This article is mainly about Hashbang. Why do you use #? of course, we can directly multi-page, just as the Site. But the biggest problem with this is that every time you switch, you need to reload the Page. We can take a few seconds to load when we go into an app, and when we use the app it's a two-second time to give the user a good card feeling.</p></p><p><p>Another advantage of WebApp is that you can reduce the pressure on the server, the background just to give Data. well, I don't really understand that. i'll Add it Later.</p></p>2. Routing (ng-route)<p><p>So how to make a single page WebApp (SPA) as a multi-page look? is how to manage these pages? It's going to be on the Route.</p></p><p><p>To a case, this is an information management system, tube user registration, logoff, user list, User details and other things.</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"><!</span></span><span style="color: #ff00ff;"><span style="color: #ff00ff;">DOCTYPE HTML</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">HTML</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Lang</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "en"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Ng-controller</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "userlist"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Head</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Meta</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">CharSet</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "UTF-8"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Head</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Body</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Div</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Table</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">TR</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">ng-repeat</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "x in data"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Ng-click</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "detail ($index)"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">TD</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>{{x.name}}:{{x.password}}<span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">a</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">href</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "#ddd/{{$index}}"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>View Details<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">a</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></</span></span><span style="color: #800000;"><span style="color: #800000;">TD</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">TR</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Table</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Div</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Body</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">HTML</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><p><p>HTML i'll Stick to part, this part is to display the user list, click the corresponding user needs to display their corresponding Information. Why this part is chosen because there is a need to do address transfer.</p></p><p><p>Then look at the corresponding pipe routing jump JS Code</p></p><pre>M1.config (["$routeProvider",<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">($routeProvider) {$routeProvider. When (</span></span>"/aaa"<span style="color: #000000;"><span style="color: #000000;">, {templateurl:</span></span>"register.html"<span style="color: #000000;"><span style="color: #000000;">, Controller:</span></span>"register"<span style="color: #000000;"><span style="color: #000000;">}). When (</span></span>"/bbb"<span style="color: #000000;"><span style="color: #000000;">, {templateurl:</span></span>"cancel.html"<span style="color: #000000;"><span style="color: #000000;">, Controller:</span></span>"cancel"<span style="color: #000000;"><span style="color: #000000;">}). When (</span></span>"/ccc"<span style="color: #000000;"><span style="color: #000000;">, {templateurl:</span></span>"userlist.html"<span style="color: #000000;"><span style="color: #000000;">, Controller:</span></span>"userlist"<span style="color: #000000;"><span style="color: #000000;">}). When (</span></span>"/ddd/:index"<span style="color: #000000;"><span style="color: #000000;">, {templateurl:</span></span>"detail.html"<span style="color: #000000;"><span style="color: #000000;">, Controller:</span></span>"detail"<span style="color: #000000;"><span style="color: #000000;">}). otherwise ({redirectto:</span></span>"/aaa"<span style="color: #000000;"><span style="color: #000000;">}); }])</span></span></pre><p><p>which</p></p><pre><pre>. When ("/ddd/:index"<span style="color: #000000;">, { templateurl:</span>"detail.html"<span style="color: #000000;">, controller:</span>"detail"<span style="color: #000000;"> })</span></pre></pre><p><p>Corresponds to the user list Page.</p></p><p><p>first, m1.config (["$routeProvider", Function ($routeProvider)]) is the process of injecting the module, because Ng-route is a module independent of NG and is a standalone JS File.</p></p><p><p>then, when the parameter of the function, the first one is the URL address (followed by: index will talk about the address of the time when the argument). Templateurl is the page address to go to (the content can be used with a template), controller write jump to the page Controllers.</p></p><p><p>The URL of the page after the jump is probably long like this: http://localhost:63342/%E8%B7%AF%E7%94%B1/index.html#/ddd</p></p><p><p>finally, the problem of address transfer, in the detail page, we only need to display a corresponding Person's information, that is, no matter who you point to the details of the display of a page, but you need to tell the details page click on which button, I should render Which. In the Code: index represents the list of users you clicked on (the number of users you clicked), and the index was indexed because the user information data was stored in an array. In the list page, this variable is called index or nothing, because it is only a pit, the value of this can be used to take this variable (of course, the name in the jump past the detail page is useful). and see how the corresponding HTML is Written.</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">TR</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">ng-repeat</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "x in data"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Ng-click</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "detail ($index)"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">TD</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>{{x.name}}:{{x.password}}<span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">a</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">href</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "#ddd/{{$index}}"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>View Details<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">a</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></</span></span><span style="color: #800000;"><span style="color: #800000;">TD</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">TR</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><p><p>Here the $index corresponds to the click of the TR in this table index, to be sure, x is the first item in Data. Index corresponds to {{$index}}</p></p><p><p>If the parameter is added, the URL of the jump page is long like this http://localhost:63342/%E8%B7%AF%E7%94%B1/index.html#/ddd/0</p></p><p><p>After more of this 0 is passed the parameter, want to take this parameter in the jump page, you can use $routeparams</p></p><pre><pre>M1.controller ("detail", ["$scope", "$routeParams",<span style="color: #0000ff;">function</span><span style="color: #000000;">($scope, $routeParams) { $scope. Index </span>=<span style="color: #000000;">$routeParams. index; }]);</span></pre></pre><p><p>This means that the index in $routeparams.index here corresponds to the one written in the When Function.</p></p><p><p>To say a digression, before I do not use the address to communicate, the way to achieve "multi-page" communication is in the $rootscope write a property to be transmitted data, after all, ng only a $rootscope. Although there is nothing wrong with this, but I always feel very uncomfortable inside.</p></p><p><p>The next section is about custom Directives.</p></p><p><p>ANGULARJS Study Notes (iii) single page webapp and routing (ng-route)</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service