在Angularjs中,要想使模板视图被缓存,可以使用ngRoute或ui-router等路由模块。具体步骤如下:
例如,使用ui-router,可以在命令行中输入以下命令进行安装:
npm install angular-ui-router
在app.js文件中配置路由,将视图模板和控制器绑定到对应的路由上:
var app = angular.module('myApp', ['ui.router']);
app.config(function($stateProvider, $urlRouterProvider) { $urlRouterProvider.otherwise('/home');
$stateProvider .state('home', { url: '/home', templateUrl: 'home.html', controller: 'homeCtrl', cache: true //此处设置视图缓存 }) .state('about', { url: '/about', templateUrl: 'about.html', controller: 'aboutCtrl', cache: true //此处设置视图缓存 }); });
在上面的代码中,可以看到,通过设置cache为true,来启用模板视图的缓存。
在index.html文件中,使用ui-view指令来展示模板视图:
以上就是解决Angularjs模板视图没有被缓存的方法。