AngularJs $watch on $location.search doesn't work when reloadOnSearch is false

前端 未结 6 694
一向
一向 2020-12-08 01:53

My routeProvider for route has reloadOnSearch set to false :

 $routeProvider
     .when(
        \"/film/list\",
         


        
6条回答
  •  情话喂你
    2020-12-08 02:45

    Stewies answer is correct, you should listen to $routeUpdate events for that since it's more efficient.

    But to answer why your watch isn't working; when you watch location.search(), you're watching if the reference that the search method returns is the same or not. And it will return the reference to the same object every time you call it, which is why your watch isn't firing. That is, even if the search parameters change, it's still the same object that is returned. To get around that, you can pass in true as the third argument to $watch. That will tell Angular to compare by value, not reference. But be careful when creating such watches, because they will consume more memory, and take longer to execute. So that's why you should do it like stewie said.

提交回复
热议问题