fixed some bugs caused by the use of $apply