问题描述
app.directive("validationMessages",['$compile',($compile: ng.ICompileService) => {
return {
require: "^form",replace: false,link: (scope,element,attrs,formCtrl) => {
const fieldName = attrs["validationMessages"];
const elementName = attrs["name"];
const formName = formCtrl.$name;
const elementFormKey = formName + "." + elementName;
let html: string = `<div class="error" ng-show="${elementFormKey}.$invalid">`;
for (const key in attrs) {
if (Object.prototype.hasOwnProperty.call(attrs,key)) {
const value = attrs[key];
switch (key) {
case "ngMax":
case "max":
html += `<small class="error ng-show="${
elementFormKey}.$error.max">
${fieldName} must be less than or equal to ${value} abcd.
</small>`;
break;
}
}
}
html += "</div>";
let template = angular.element(html);
let parser = $compile(template);
let parsed = parser(scope);
element.after(parsed);
}
};
}]);
<input class="form-control"
ng-model="Vm.c0"
name="C0"
validation-messages="Constant C0"
max="{{Vm.maxValue}}"
/>
问题是,模板未反映对VM.maxValue
的更改。
实际上,link
函数仅运行一次。我需要watch
吗?
为什么呢?
更新:
Finallay我想现在问题出在哪里了-{{ }}
表达式:
max =“ {{Vm.maxValue}}”
还有另一种方式传递Vm.maxValue
吗?
解决方法
实际上,不会监视带有诸如{{}}
之类的表达式绑定的属性的更改。
添加scope.watch()
会有所帮助。