Gis*_*zmo 5 javascript angularjs angularjs-ng-include
我正在考虑一些小部件并且$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)
} ])
Run Code Online (Sandbox Code Playgroud)
包装器的HTML:
<ng-include
src="'partials/getRecipientWidget.html'"
ng-controller="getRecipientWidgetController"
ng-init="recipient=cert"> <!-- ng-init doesn't influence the bug. -->
</ng-include>
Run Code Online (Sandbox Code Playgroud)
HTML partials/getRecipientWidget.html:
<md-text-float ng-model="addressInput"></md-text-float>
Run Code Online (Sandbox Code Playgroud)
我怀疑有一些范围伏都教正在进行中?我离开了ng-init以明确我想要实现的目标:构建一个明显更复杂,可重用的小部件,在这个实例中可以$scope.cert作为它recipient.
这可能是因为ng-include将在所包含的HTML创建一个新的继承范围,因此$scope.addressInput在你的控制器是不是同一基准$scope.addressInput中getRecipientWidget.html
好吧,这不容易解释,但你应该放在ng-controllerHTML之内getRecipientWidget.html(而不是上面包含它的div),或者你可以使用一个对象,something.addressInput而不是原始的addressInput,避免原始类型的引用问题(数量/串).