添加范围对象侦听器



我有$scope.number = 1 .我想添加一个侦听器来更新路径。

我的尝试不起作用:

function LocationController($scope, $location) {
   $scope.$watch($scope.number, function(path) {
     $location.path('/'+$scope.number);
   });
}

另外,$scope.number如何称呼对象,子对象?

试试

function LocationController($scope, $location) {
   $scope.$watch('number', function(newNumber) {
     $location.path('/'+newNumber);
   });
}

阅读文档以了解要$watch的参数

最新更新