什么是AngularJS路线的地步? [英] What is the point of AngularJS routes?

查看:133
本文介绍了什么是AngularJS路线的地步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林创建一个网站,我选择了做它AJ。
我有两个文件夹:结果
网关=>从一个MySQL数据库retrive数据和回声的数据作为JSON一些PHP文件。结果
查看=>部分HTML文件,它们基本上是每个页面的模板。例如用户,项目等。

Im creating a website and I chose to do it in AJ. I have two folders:
Gateways => some php files which retrive data from a mysql db and echos the data as json.
Views => Partial html files which are basically the template for each page. eg Users, Items, etc.

然后我有HANDELS请求过程和路由一个index.js文件:

then I have a index.js file which handels the request process and routing:

angular.module('Index',['addon'],function($routeProvider,$locationProvider){
    $locationProvider.html5Mode(true).hashPrefix("!");
    $routeProvider.otherwise({
        templateUrl: '/views/index.html',
        controller: "index"
    }).when("/items/",{
        templateUrl: '/views/items.html',
        controller: "items"
    })
}).
controller("index",function($scope,$http){

    $scope.users = [];
    $http.get("gateways/list.php").
    success(function(d){
        console.log(d);
        if(angular.isArray(d)){
            $scope.users = d;
        }
    });

}).
controller("items",function($scope,$http,$routeParams){
    $scope.items = [];
    $http.get("gateways/single.php").
    success(function(d){
        if(angular.isArray(d)){
            $scope.items = d;
        }
    });

}).

什么是这一切的路线提供商AJ除了其优美的地步?
不要他们只是减慢网站,因为请求的数量?
我可以只写PHP code从网关直接在模板文件吗?
我这样做是错误的方式?

What is the point of all this route providers in AJ except its elegancy? Dont they just slow down the site because of number of the requests? I could just write the php code from the gateways files directly in the templates files? Am I doing it in the wrong way?

推荐答案

您可能缺少使用框架,如AngularJS的地步。他们大多是单页的应用程序和Ajax密集。让我开始这一请求的数量并不真正慢下来的应用程序。还记得Gmail的使用方式是在第一次没有Ajax?是不是比目前的执行速度更快?

You might be missing the point of using frameworks like AngularJS. They are mostly for Single Page application and ajax intensive. And let me begin that the number of requests don't really slow down application. Remember how Gmail used to be at first without Ajax ? Was it faster than the current implementation ?

现在,当你建立一个单一的页面应用程序,应用程序发送到REQ服务器和服务器以JSON或XML响应。在像AngularJS或任何其他框架,服务器发送JSON响应,我们使它们使用客户端模板或DOM。这节省从服务器端巨大的资源,因为服务器不必解析阵列和呈现的模板。所以,如果服务器不得不使具有100行的表格,带宽发送给您的浏览器可能是20KB,而只有JSON可能是5KB和一个字符串或DOM模板,1KB做渲染。所以,你实际上有你的访问者的浏览器做了很多工作。这是所有关于计算/计算。想想一个统计表格,其中有列和一些计算值是在数据网格psented $ P $的。随着客户端框架,你的服务器将不仅仅是应对为JSON和浏览器(使用JavaScript)将解析,计算和渲染它的模板。

Now, when you build a single page application, the app sends req to server and server responds in JSON or XML. In frameworks like AngularJS or any other, server sends the JSON response and we render them using client side templates or DOM. This saves enormous resource from the server side since server doesn't have to parse the array and render the template. So if server had to render a table which has 100 rows, the bandwidth to send to your browser might be 20KB whereas only JSON might be 5KB and a string or DOM template with 1KB to do the rendering. So, you're actually having your visitor's browser do a lot of job. It's all about calculations/computations. Think of a statistics table where there are columns and some computed values to be presented in a data grid. With Client side frameworks, your server would just respond as JSON and browser (with JavaScript) will parse,calculate and render it in template.

作为使用routerProvider的问题,它的书签和使用的浏览器的导航键是必不可少的。就像一个传统的URL有一个观点,在客户端应用程序的URL都有这应该是可标记,并应支持导航视图。

As for the question of using a routerProvider, it's essential to bookmark and use browser navigation button. Just like a traditional url has a view, a URL in client side app has a view which should be bookmarkable and should support navigation.

这篇关于什么是AngularJS路线的地步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆