【问题标题】:AngularJS templating and making it faster?AngularJS 模板化并使其更快?
【发布时间】:2014-05-23 21:56:09
【问题描述】:

我想结合 AngularJS 和 Django 来制作单页应用程序。一般来说,我有索引页面(搜索)和包含更多子页面的详细信息页面。

这就产生了问题。我有一个控制器(有关详细信息,它正在获取有关所选对象的信息)和其他控制器用户使用 $controller 函数的主控制器。

看起来像:

.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";
  }])

还有我的网址:

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: '/'});
}]);

details.htmlpage 上,我有一个菜单,但加载时出现问题,每次我从 InfoCtrl 更改为 SecondCtrl 时,我的菜单都会被刷新,我可以看到(不到半秒)。很烦人。

有什么方法可以阻止加载这些模板,只加载部分模板,但要更改 URL(我需要从复制的 url 等访问它)?

【问题讨论】:

    标签: javascript django angularjs


    【解决方案1】:

    你可以embed你的模板在已经渲染的页面中作为text/ng-template类型的脚本:

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

    Here 是个笨蛋。


    您不需要在控制器中调用$controller()。您的 Angular 应用程序将通过在您的 html 中包含这样的指令来实例化所需的控制器:

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

    【讨论】:

    • 是的,但我有,我只是使用 ng-include,但我的网站每次都在刷新
    • 你把它嵌入到页面中了吗?它会变慢的唯一原因是:1)浏览器将模板 ajax 到页面中,2)有坏/慢代码。假设 #1 是您的问题,嵌入式模板应该可以解决它。
    • 是的,我有包含菜单的 details.html 页面,你看,我将部分绑定到 $scope,然后我在我的页面上显示该部分,但它太慢了。 是的,浏览器每次都发送新请求。如何解决它
    • 这一行:$controller('BuildingDetailsCtrl', {$scope: $scope, $routeParams: $routeParams, buildingsRepository: buildingsRepository, $location: $location}) -- 这是在做什么?在您的模板 html 中,您可以通过 ng-controller 指令指定控制器,如下所示:&lt;div ng-controller="BuildingDetailsCtrl"&gt;
    • 我的答案应该通过将该模板嵌入页面来解决每次重新加载的问题。一旦加载一次,它也会被编译到 templateCache 中(再优化一点)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多