【发布时间】:2015-05-20 13:35:42
【问题描述】:
我正在分解一些小部件,$watch 表达式在一个文件中完美运行,但现在我将相关控制器部分移动到一个新控制器中,并将标记移动到一个新 html 中,$watch 只触发一次在初始化之后,但不是在编辑相关输入时键入。
JS:
app.controller('getRecipientWidgetController', [ '$scope', function($scope) {
console.log("controller initializing")
var testReceivingAddress = function(input) {
console.log("change detected")
}
$scope.$watch("addressInput", testReceivingAddress)
} ])
包装器的HTML:
<ng-include
src="'partials/getRecipientWidget.html'"
ng-controller="getRecipientWidgetController"
ng-init="recipient=cert"> <!-- ng-init doesn't influence the bug. -->
</ng-include>
partials/getRecipientWidget.html的HTML:
<md-text-float ng-model="addressInput"></md-text-float>
我怀疑有一些范围的巫术正在发生?我留下了ng-init,以明确我想要实现的目标:构建一个明显更复杂、可重用的小部件,在这种情况下,它可以在$scope.cert 上作为recipient 工作。
【问题讨论】:
-
开玩笑,当你注入 rootscope 并在上面调用 $watch 会发生什么?
-
@dustmouse 没有变化。完全相同的错误/行为。
-
正如floribon在他的回答中所说,了解所谓的“点规则”。
标签: javascript angularjs angularjs-ng-include