В моем $onInit
у меня есть $watch
:
public $onInit() {
const name = this.$state.current.name;
console.log(name);
this.$scope.$watch(name, (oldVal, newVal) => {
console.log(oldVal, newVal);
console.log(this.$state.current.name;
});
}
Когда я запускаю этот код, oldVal, newVal
оба равны undefined
, а значение this.$state.current.name
одинаково в области видимости$watch
и вне его.
Когда я запускаю код без выражения name
:
public $onInit() {
const name = this.$state.current.name;
console.log(name);
this.$scope.$watch((oldVal, newVal) => {
console.log(oldVal, newVal);
console.log(this.$state.current.name);
});
}
$watch
постоянно запускается.oldVal
равно Scope {$id: 353, .... etc
, но newVal
не определено.Но this.$state.current.name
теперь обновляется внутри области действия $watch
.
Таким образом, значение this.$scope.current.name
действительно изменяется.Но когда я использую это как выражение в $watch
, oldVal, newVal
оба не определены, а this.$state.current.name
не изменяется внутри $watch
.
Похоже, я что-то делаю не такв watchExpression
.Есть предложения?