<div ng-controller="theController">
content does not matter
</div>
因此,控制器被实例化多次。
这是一个问题,因为如果我在控制器中放置了一个监视器
theController = function($scope) {
$scope.$on('myVar', function() {
// run one time for each time the template is repeated
})
}
有没有什么方法可以避免这种情况?谢谢提前。
更新
好的,我会尽量更清楚。
也许我有一个表单,是根据异步请求的响应动态构建的。
<form ng-controller="formController">
<div ng-repeat="f in fields">
<ng-inclide src="f.fields"></ng-include>
</div>
</form>
控制器类似于:
function formController($scope) {
$scope.fields = [{ template:'', ... }];
// data come from an ajax request...
// here are static for the sake of simplicity.
}
我不知道表单中会添加哪些字段。
表单字段结构存储在 HTML 局部文件中,大概是这样的:
<div ng-controller="inputController">
<label> .... </label>
<input type="text" .... />
</div>
或者
<div ng-controller="selectController">
<label> .... </label>
<select>
....
</select>
</div>
function selectController($scope){
$scope.$on("myCustomEvent", function(event) {
cionsole.info("Options were updated");
});
}
当表单有多个或