简体   繁体   English

AngularJS模板化并使其更快?

[英]AngularJS templating and making it faster?

I want to use AngularJS in combination with Django to make single-page application. 我想将AngularJS与Django结合使用以制作单页应用程序。 In general, I have index page (search) and details page with more sub-pages. 通常,我有索引页面(搜索)和详细信息页面,以及更多子页面。

And that makes a problem. 这就是一个问题。 I have one controller (for details and it is getting info about object which is chosen) and other controllers user that main controller using $controller function. 我有一个控制器(有关详细信息,它正在获取有关所选择对象的信息),其他控制器用户使用$controller函数作为主控制器。

It looks like: 看起来像:

.controller('BuildingDetailsCtrl', ['$scope', '$routeParams', 'buildingsRepository', '$location',
   function($scope, $routeParams, buildingsRepository, $location) {

        $scope.details = null;
        $scope.menu = templatesFolder + "buildings/menus/";
        $scope.currentUrl = $location.url();
        $scope.loading = true;

        buildingsRepository.getDetails($routeParams.slug).then(function(res) {
            $scope.details = res.data[0];
            $scope.loading = false;
        });

        $scope.alert = {"type": null, "message": null};

  }])

  .controller('SecondCtrl', ['$scope', '$routeParams', 'buildingsRepository', '$location', '$controller',
   function($scope, $routeParams, buildingsRepository, $location, $controller) {

        $controller('BuildingDetailsCtrl', {$scope: $scope, $routeParams: $routeParams, buildingsRepository: buildingsRepository, $location: $location})
        $scope.partial = templatesFolder + "buildings/details/info.html";
  }])

and my urls: 和我的网址:

config(['$routeProvider', function($routeProvider) {

  $routeProvider.when('/', {templateUrl: templatesFolder + "buildings/index.html", controller: 'UserBuildingsCtrl'});
  $routeProvider.when('/details/:slug', {templateUrl: templatesFolder + "buildings/details.html", controller: 'BuildingInfoCtrl'});
  $routeProvider.when('/test/:slug', {templateUrl: templatesFolder + "buildings/details.html", controller: 'SecondCtrl'});
  $routeProvider.when('/contact/:slug', {templateUrl: templatesFolder + "buildings/details.html", controller: 'BuildingContactCtrl'});
  $routeProvider.otherwise({redirectTo: '/'});
}]);

On that details.html page, I have one menu, but problem is loading, and everytime I change from, for example, InfoCtrl to SecondCtrl, my menu is being refreshed and I can see that (less than half second). 在该details.html页面上,我有一个菜单,但是问题正在加载,并且每次从例如InfoCtrl更改为SecondCtrl时,我的菜单都会刷新,并且可以看到(不到半秒)。 It is irritating. 这很烦人。

Is there any way to prevent loading of those templates, to load just partial, but with changing URL (I need it to be accessed from copied url etc)? 有什么方法可以防止加载那些模板,仅加载部分模板,但是要更改URL(我需要从复制的url等访问它)?

You can embed your template in the already-rendered page as a script of type text/ng-template : 您可以模板作为text/ng-template类型的脚本嵌入到已渲染的页面中:

<script type="text/ng-template" id="details.html">
  <p>hello world</p>
</script>

Here is a plunk. 是一个小家伙。


You should not need to call $controller() in your controller. 您无需在$controller()调用$controller() You angular app will instance the controller needed by including a directive in your html like this: 您的角度应用程序将通过在html中包含如下指令来实例化所需的控制器:

<div ng-controller="BuildingDetailsCtrl">
   ...
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM