理解在successCallback中设置的$ http.get和变量

时间:2016-09-09 18:07:44

标签: angularjs http controller get

以下是我正在处理的当前控制器的一部分:

var vm = $scope;

/* get all teachers from database */
$http.get('app/php/db.php?action=get&table=data')
    .then(function successCallback(response){
        vm.data       = response.data;
        vm.totalItems = response.data.length;
    }, function errorCallback(response){

    });

console.log(vm.data);

控制台说数据未定义。如何设置能够读取successCallback中设置的变量?

1 个答案:

答案 0 :(得分:0)

您记录它的方式,它始终是$http.get('app/php/db.php?action=get&table=data') .then(function successCallback(response){ vm.data = response.data; vm.totalItems = response.data.length; console.log(vm.data); //here }, function errorCallback(response){ }); 。因为您的电话是$watch。它可能需要2毫秒,200毫秒 - 不确定何时会收到服务器的响应。

您可以在successCallback

中记录它
$scope.$watch('vm.data', function(newData){
   console.log(newData.length);
});

您可以在控制器中执行import tkinter as tk class App(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) self.geometry("700x500") self.canvas = tk.Canvas(self,width=700, height=50, bg='white') self.canvas.create_rectangle(0, 0, 700, 50, width=0, fill='dark slate gray') self.canvas.pack() self.button = tk.Button(self,bg="gray",text="x",font=("Times", "24", "bold italic"),command=lambda: self.destroy(),border=0) self.button.place(x=645,y=5,width=45,height=40) self.overrideredirect(True) self.button.bind("<Enter>", self.on_enter) self.button.bind("<Leave>", self.on_leave) self.canvas.bind("<ButtonPress-1>", self.StartMove) self.canvas.bind("<ButtonRelease-1>", self.StopMove) self.canvas.bind("<B1-Motion>", self.OnMotion) def on_enter(self, event): self.button.configure(bg="firebrick") def on_leave(self, enter): self.button.configure(bg="gray") def StartMove(self, event): self.x = event.x self.y = event.y def StopMove(self, event): self.x = None self.y = None def OnMotion(self, event): deltax = event.x - self.x deltay = event.y - self.y x = self.winfo_x() + deltax y = self.winfo_y() + deltay self.geometry("+%s+%s" % (x, y)) app=App() app.mainloop()

someElement.getBoundingClientRect()