AngularJS頁面訪問時出現(xiàn)頁面閃爍問題的解決
來源:易賢網(wǎng) 閱讀:1424 次 日期:2016-08-02 14:23:03
溫馨提示:易賢網(wǎng)小編為您整理了“AngularJS頁面訪問時出現(xiàn)頁面閃爍問題的解決”,方便廣大網(wǎng)友查閱!

我們知道在應(yīng)用的頁面或者組件需要加載數(shù)據(jù)時,瀏覽器和angular渲染頁面都需要消耗一定的時間。這里的間隔可能很小,甚至讓人感覺不到區(qū)別;但也可能很長,這樣會導(dǎo)致讓我們的用戶看到了沒有被渲染過的頁面。

這種情況被叫做Flash Of Unrendered Content (FOUC)(K)?and is always unwanted.下面我們將要介紹幾個不同的方式防止這種情況發(fā)生在我們的用戶身上。

1、ng-cloak

ng-cloak指令是angular的內(nèi)置指令,它的作用是隱藏所有被它包含的元素:

<div ng-cloak>

 <h1>Hello {{ name }}</h1>

</div>

Ng-cloak實現(xiàn)原理為一個directive,頁面初始化是在DOM的heade增加一行CSS代碼,如下:

<pre class= “prettyprint linenums”>

  [ng\:cloak],[ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak{

  Display:none ! important;

}

</pre>

<pre class= “prettyprint linenums”>

  [ng\:cloak],[ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak{

  Display:none ! important;

}

</pre>

Angular將帶有ng-cloak的元素設(shè)置為display:none.

在等到angular解析到帶有ng-cloak節(jié)點的時候,會把元素上ng-cloak  attribute和calss同時remove掉,這樣就防止了節(jié)點的閃爍。如下:

<script type =”text/ng-template” id =”scenario.js-150”>

  It(‘should remove the template directive and css class',function(){

 Expect(element(‘.doc-example-live #template1').attr(‘ng-cloak'))

  not().toBeDefined();

   Expect(element(‘.doc-example-live #template2').attr(‘ng-cloak')).

Not().toBeDefined();

});

</script>

<script type =”text/ng-template” id =”scenario.js-150”>

  It(‘should remove the template directive and css class',function(){

 Expect(element(‘.doc-example-live #template1').attr(‘ng-cloak'))

  not().toBeDefined();

   Expect(element(‘.doc-example-live #template2').attr(‘ng-cloak')).

Not().toBeDefined();

});

</script>

2、ng-bind

ng-bind是angular里面另一個內(nèi)置的用于操作綁定頁面數(shù)據(jù)的指令。我們可以使用ng-bind代替{{ }}的形式綁定元素到頁面上;

使用ng-bind替代{{  }}可以防止未被渲染的{{ }}就展示給用戶了,使用ng-bind渲染的空元素替代{{ }}會顯得友好很多。

上面的例子可以重寫成下面那樣,這樣就可以防止頁面出現(xiàn){{ }}了

<div>

 <h1>Hello <span ng-bind="name"></span></h1>

</div>

3、resolve

當(dāng)在不同的頁面之間使用routes(路由)的時候,我們有另外的方式防止頁面在數(shù)據(jù)被完全加載到route之前被渲染。

在route(路由)里使用resolve可以讓我們在route(路由)被完全加載之前獲取我們需要加載的數(shù)據(jù)。當(dāng)數(shù)據(jù)被加載成功之后,路由就會改變而頁面也會呈現(xiàn)給用戶;數(shù)據(jù)沒有被加載成功route就不會改變, the $routeChangeError event will get fired.【$routeChangeError事件就(不)會被激活?】

angular.module('myApp', ['ngRoute'])

.config(function($routeProvider) {

 $routeProvider

 .when('/account', {

 controller: 'AccountCtrl',

 templateUrl: 'views/account.html',

 resolve: {

  // We specify a promise to be resolved

  account: function($q) {

  var d = $q.defer();

  $timeout(function() {

   d.resolve({

   id: 1,

   name: 'Ari Lerner'

   })

  }, 1000);

  return d.promise;

  }

 }

 })

});

resolve 項需要一個key/value對象,key是resolve依賴的名稱,value可以是一個字符串(as a service)或者一個返回依賴的方法。

resolve is very useful when the resolve value returns a promise that becomes resolved or rejected.

當(dāng)路由加載的時候,resolve參數(shù)里的keys可以作為可注入的依賴:

angular.module('myApp')

.controller('AccountCtrl', 

 function($scope, account) {

 $scope.account = account;

});

我們同樣可以使用resolve key傳遞$http方法返回的結(jié)果,as $http returns promises from it's method calls:

angular.module('myApp', ['ngRoute'])

.config(function($routeProvider) {

 $routeProvider

 .when('/account', {

 controller: 'AccountCtrl',

 templateUrl: 'views/account.html',

 resolve: {

  account: function($http) {

  return $http.get('http://example.com/account.json')

  }

 }

 })

});

推薦定義一個獨立的service的方式來使用resolve key,并且使用service來相應(yīng)返回所需的數(shù)據(jù)(這種方式更容易測試)。要這樣處理的話,我們需要創(chuàng)建一個service:

首先,看一下accountService:

angular.module('app')

.factory('accountService', function($http, $q) {

 return {

 getAccount: function() {

  var d = $q.defer();

  $http.get('/account')

  .then(function(response) {

  d.resolve(response.data)

  }, function err(reason) {

  d.reject(reason);

  });

  return d.promise;

 }

 }

})

定義好service之后我們就可以使用這個service來替換上面代碼中直接調(diào)用$http的方式了:

angular.module('myApp', ['ngRoute'])

.config(function($routeProvider) {

 $routeProvider

 .when('/account', {

 controller: 'AccountCtrl',

 templateUrl: 'views/account.html',

 resolve: {

  // We specify a promise to be resolved

  account: function(accountService) {

  return accountService.getAccount()

  }

 }

 })

});

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:AngularJS頁面訪問時出現(xiàn)頁面閃爍問題的解決
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)