2017-04-04 64 views
0

我可以看到更新的數據,當我刷新頁面。當我使用$ state.reload它工作正常。所以如何解決這個問題,而不使用$ tate.reload我怎麼才能更新數據,當我點擊保存按鈕。 這裏是我的javascript代碼:如何使用angularjs更新數據?

vm.myFuncUpdate= function() { 
    if (!vm.id) { vm.id= vm.infoFunc.date; } 
    if (!vm.pid) { vm.pid= vm.infoFunc.person} 

    mySrvc.myPostData(id,pid,function (response, status) { 
     console.log(response); 
     vmCancel(); 

    }); 

} 

HTML代碼:

<input type="button" value="Save" ng- 
    click="myFuncUpdate()" /> 

服務:

this.myPostData= function (id, pid, funCallBack) { 
    $http({ 
     method: "POST", 
     url: "http://myapp/api/profile/details", 
     headers: { 
      'Content-Type': 'application/json; charset=utf-8', 
      'dataType': 'json' 
     }, 
     data: { id: id, pid: pid } 
    }).success(function (response, status) { 
     funCallBack(response, status); 

    }).error(function (reason, status) { 
     if (status == 400) 
      alert(JSON.stringify(reason)); 
     else if (status == 404) 
      alert("Something wrong!"); 
     else 
      alert("Something Wrong"); 

    }); 
} 

任何幫助,將不勝感激。

+0

真的不清楚你在問什麼。「當我點擊保存按鈕時,我必須更新數據。但問題是,數據被更新了,但是我當我刷新頁面時,可以看到更新的數據。「。這是否意味着你希望**看到更新,或者你不需要**? – Claies

+0

我可以看到你刷新頁面後,只有pdated,爲此我用$ state.reload.Is有任何可能的方式,而不是$ state.reload(); @Claies – pbsbr

+0

好吧,它*聽起來像*您需要更新'.success'函數中的客戶端數據。作爲一個附註,你不應該使用'.success',它已經被棄用,並且從新的角度版本中被移除。 – Claies

回答

0

我猜你有一個API調用將獲取並顯示更新後的數據嗎? 所以你只需要當您使用此API「http://myapp/api/profile/details

var init = function(){ 
     APIServices.profileData.success(function(data) { 
     self.newProfileData = data; 
     //console.log(self.newProfileData); 
    })  
    } init(); 
$rootScope.$on('update-Data', function(){ 
     init(); 
    }); 

this.myPostData= function (id, pid, funCallBack) { 
$http({ 
    method: "POST", 
    url: "http://myapp/api/profile/details", 
    headers: { 
     'Content-Type': 'application/json; charset=utf-8', 
     'dataType': 'json' 
    }, 
    data: { id: id, pid: pid } 
}).success(function (response, status) { 
    funCallBack(response, status); 
    **$rootScope.$broadcast('update-Data');** 

}).error(function (reason, status) { 
    if (status == 400) 
     alert(JSON.stringify(reason)); 
    else if (status == 404) 
     alert("Something wrong!"); 
    else 
     alert("Something Wrong"); 

}); 

}

這是更新模板的方法之一,PS再次打電話說:我認爲u有一個提取API,如果不那麼我認爲你可以使用$ watch來觀看模型

+0

對不起,我不能讓你在哪裏我必須調用init();函數 – pbsbr

+0

你有一個fetch API嗎?這將獲取你更新的配置文件數據? –

+0

是的,我有api這將獲取更新的數據 – pbsbr