ngModel 值未反映在视图中 - 已在指令中更改
ngModel value not reflected on view - changed in directive
值最初在控制器中设置为 false
$scope.isChangeUnsaved = false;
视图中的值
Changes Status : {{isChangeUnsaved }} // false initially
我正在尝试更改指令中 ngModel 的值。该值已更改,但未反映在视图中。这是我正在使用的代码
<form ngc-reload ng-model="isChangeUnsaved">
指令
angular.module("biApp").directive("ngcReload", ["$parse","$log", function ($parse,$log) {
return {
restrict: "A",
scope: {
ngModel:"="
},
link: function(scope, element, attrs, controller){
$JQ(".mainmenu a").click(function(e){
e.preventDefault();
scope.$apply(function() {
scope.ngModel = !scope.ngModel;
});
console.log("scope.ngModel",scope.ngModel); // Outputs true - value is changed
});
}
}
}]);
值未更改
Changes Status : {{isChangeUnsaved }} // false even after changed in directive
试试这个
angular.module("biApp").directive("ngcReload", ["$parse","$log", function ($parse,$log) {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, iElement, iAttrs, ngModel) {
$JQ(".mainmenu a").click(function(e){
e.preventDefault();
ngModel.$setViewValue(true);
ngModel.$render();
});
}
}
}]);
值最初在控制器中设置为 false
$scope.isChangeUnsaved = false;
视图中的值
Changes Status : {{isChangeUnsaved }} // false initially
我正在尝试更改指令中 ngModel 的值。该值已更改,但未反映在视图中。这是我正在使用的代码
<form ngc-reload ng-model="isChangeUnsaved">
指令
angular.module("biApp").directive("ngcReload", ["$parse","$log", function ($parse,$log) {
return {
restrict: "A",
scope: {
ngModel:"="
},
link: function(scope, element, attrs, controller){
$JQ(".mainmenu a").click(function(e){
e.preventDefault();
scope.$apply(function() {
scope.ngModel = !scope.ngModel;
});
console.log("scope.ngModel",scope.ngModel); // Outputs true - value is changed
});
}
}
}]);
值未更改
Changes Status : {{isChangeUnsaved }} // false even after changed in directive
试试这个
angular.module("biApp").directive("ngcReload", ["$parse","$log", function ($parse,$log) {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, iElement, iAttrs, ngModel) {
$JQ(".mainmenu a").click(function(e){
e.preventDefault();
ngModel.$setViewValue(true);
ngModel.$render();
});
}
}
}]);