2015-09-14 34 views
2

我是新來的角,我一直在玩,試圖瞭解它是如何工作的。我正在做一個簡單的應用程序,其中有人可以通過簡單的html界面添加用戶,並使用SQLite將其存儲在數據庫中,然後他們可以編輯或刪除它們。

所以我試圖scope.apply()瞭解本手冊使用$所以我也爲創建這樣一個場景,我將需要使用它的以下內容:

上的功能,我存儲一些變量在localStorage的,然後我手動打開HTML:

localStorage.setItem('idUsuario',response.records[0].idUsuario); 
    localStorage.setItem('dato_Nombre',response.records[0].nombre); 
    localStorage.setItem('dato_ApPat',response.records[0].apPat); 
    localStorage.setItem('dato_ApMat',response.records[0].apMat); 
    localStorage.setItem('dato_Direccion',response.records[0].direccion); 
    localStorage.setItem('dato_Tel',response.records[0].telefono); 
    localStorage.setItem('dato_email',response.records[0].correo); 
    //debugger; 
    location.href = "editClient.html";} 

在我打開HTML,我宣佈我CONTROLER接收存儲的數據,併爲其分配到NG-模型變量我裏面聲明NG-的init()對一些文本輸入:

$scope.init = function() { 
    $scope.idUsuario=localStorage.getItem("idUsuario"); 
    $scope.dato_Nombre=localStorage.getItem("dato_Nombre"); 
    $scope.dato_ApPat=localStorage.getItem("dato_ApPat"); 
    $scope.dato_ApMat=localStorage.getItem("dato_ApMat"); 
    $scope.dato_Tel=localStorage.getItem("dato_Tel"); 
    $scope.dato_Direccion=localStorage.getItem("dato_Direccion"); 
    $scope.dato_email=localStorage.getItem("dato_email"); 

    localStorage.clear(); 
} 



<div class="list"> 
      <label class="item item-input item-stacked-label"> 
       <span class="input-label" >Nombre</span> 
       <input type="text" placeholder="Escriba su nombre" ng-model="dato_Nombre" > 
      </label> 
      <label class="item item-input item-stacked-label"> 
        <span class="input-label">Apellido Paterno</span> 
       <input type="text" placeholder="Escriba su apellido paterno" ng-model="dato_ApPat"> 
      </label> 
      <label class="item item-input item-stacked-label"> 
        <span class="input-label">Apellido Materno</span> 
       <input type="text" placeholder="Escriba su apellido materno" ng-model="dato_ApMat"> 
      </label> 
      <label class="item item-input item-stacked-label"> 
        <span class="input-label">Dirección</span> 
       <input type="text" placeholder="Escriba su dirección" ng-model="dato_Direccion"> 
      </label> 
      <label class="item item-input item-stacked-label"> 
      <span class="input-label">Teléfono</span> 
      <input type="text" placeholder="Escriba su teléfono" ng-model="dato_Tel"> 
      </label> 
      <label class="item item-input item-stacked-label"> 
        <span class="input-label">Email</span> 
       <input type="text" placeholder="Escriba su correo" ng-model="dato_email"> 
      </label> 
     <center> 
      <button class="button button-possitive" ng-click="editarCliente()">Actualizar</button> 
     </center>  
     </div> 
當表單加載輸入實際上是充滿了我從localStorage的帶來的數據

所以,後來我修改文本鍵入別的東西和調用,它返回每個NG-模型哪像預期的,具有數據功能沒有更新到我在輸入中輸入的內容,仍然有從localStorage分配的數據,因此需要使用$ scope.apply()。

我的問題是,無論我在哪裏嘗試使用它,我總是會收到一個錯誤,表示應用程序已在運行,我無法弄清楚如何使用它來更新我的文本輸入內容。

回答

2

$範圍。$應用火災的角度消化週期,但因爲你的變化是一個角度應用程序內部發生的事情,有一個週期已經運行和處理你的數據,這就是爲什麼你得到這個錯誤,因爲你不應該2個摘要循環同時執行。 $ scope。當角度環境發生一些變化時,$ apply是有用的。例如,當你需要集成不友好的角度組件,如jquery插件或其他UI元素來處理它們的角色世界的事件。如果您想更新任何這些元素事件中的角度應用程序,您需要使用$ scope。$ apply,如果您選擇使用setTimeout或setInterval而不是提供給角度的$ timeout或$ interval服務,也會發生這種情況,因爲函數的執行會發生在角度摘要循環之外。

$.ajax({ 
url:'http://lslsls.com', 
method: 'get' 
}) 
.success(function(data){ 
    var scope = angular.element($('#elementId')).scope(); 
    scope.returnedData=data; 
    scope.$apply(); 
}); 
$

不重視對jQuery的一部分,但請注意,在成功的功能範圍檢索和更新的,如果我不使用範圍。適用()那裏,值止跌」 t直到發生一些其他變化,並且強制消化循環,可以表示範圍