angular 指令不会更新与控制器的双向绑定
angular directive won't update two-way binding with controller
我有一个模态指令,它应该在按下按钮时变为可见。但是,负责确定是否应打开与指令双向绑定的模态的值似乎没有更新。有什么想法吗?
这里是指令 JS:
function modalDialogSelect() {
return {
restrict: 'AE',
scope: {
show: '='
},
replace: true,
transclude: true,
link: function(scope, element, attrs) {
scope.dialogStyle = {};
scope.hideModal = function() {
scope.show = false;
};
},
templateUrl: "./views/directive_templates/select_modal.html"
};
}
指令内HTMLURL:
<div class='ng-modal' ng-show='show'>
<div class='ng-modal-overlay' ng-click='hideModal()'></div>
<div class='ng-modal-dialog' ng-style='dialogStyle'>
<p>Select New Customer</p>
</div>
</div>
</div>
单击并打开模式的按钮:
<button ng-click='selectCustomer = true;'>SELECT/CREATE CUSTOMER</button>
和我一般的 modal-dialog-select 指令 HTMl
<modal-dialog-select show='selectCustomer'></modal-dialog-select>
知道为什么 scope.show 没有在我的指令中更新吗?谢谢!
这可能是 javascript 按值传递的有趣问题,因为 var 是 bool。老实说,我不擅长解释这个问题的根本原因。
尝试将其作为对象传递
ng-click='selectCustomer.value = true'
那么您的表演范围 属性 将是 scope.show.value。
似乎工作正常...
function modalDialogSelect() {
return {
restrict: 'AE',
scope: {
show: '='
},
replace: true,
transclude: true,
link: function(scope, element, attrs) {
scope.dialogStyle = {};
scope.hideModal = function() {
scope.show = false;
};
},
template: "<div class='ng-modal' ng-show='show'><button ng-click='hideModal()'>Here's the Modal</button></div>"
};
}
angular.module('test', [])
.controller('testCtrl', function() {})
.directive('modalDialogSelect', modalDialogSelect);
<script src="https://code.angularjs.org/1.3.0/angular.js"></script>
<div ng-app="test">
<div ng-controller="testCtrl as vm">
<button ng-click='vm.selectCustomer = true;'>SELECT/CREATE CUSTOMER</button>
<modal-dialog-select show='vm.selectCustomer'></modal-dialog-select>
</div>
</div>
您在生成模态模板时遇到问题
Error: Template must have exactly one root element. was:
<div class='ng-modal' ng-show='show'>
<div class='ng-modal-overlay' ng-click='hideModal()'></div>
<div class='ng-modal-dialog' ng-style='dialogStyle'>
<p>Select New Customer</p>
</div>
</div>
</div>
删除最后一个 </div>
后一切正常。
var app = angular.module("myApp", []);
app.controller('ParentController', function($scope) {
$scope.selectCustomer = false;
});
app.directive('modalDialogSelect', function() {
return {
restrict: 'AE',
scope: {
show: '='
},
replace: true,
transclude: true,
link: function(scope, element, attrs) {
scope.dialogStyle = {};
scope.hideModal = function() {
scope.show = false;
};
},
templateUrl: "modal.html"
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.min.js"></script>
<div ng-app="myApp" ng-controller="ParentController">
<p>Flag is: {{selectCustomer}}</p>
<button ng-click='selectCustomer = !selectCustomer'>SELECT/CREATE CUSTOMER</button>
<modal-dialog-select show='selectCustomer'></modal-dialog-select>
<script type="text/ng-template" id="modal.html">
<div class='ng-modal' ng-show='show'>
<div class='ng-modal-overlay' ng-click='hideModal()'></div>
<div class='ng-modal-dialog' ng-style='dialogStyle'>
<p>Select New Customer</p>
</div>
</div>
</script>
</div>
我有一个模态指令,它应该在按下按钮时变为可见。但是,负责确定是否应打开与指令双向绑定的模态的值似乎没有更新。有什么想法吗?
这里是指令 JS:
function modalDialogSelect() {
return {
restrict: 'AE',
scope: {
show: '='
},
replace: true,
transclude: true,
link: function(scope, element, attrs) {
scope.dialogStyle = {};
scope.hideModal = function() {
scope.show = false;
};
},
templateUrl: "./views/directive_templates/select_modal.html"
};
}
指令内HTMLURL:
<div class='ng-modal' ng-show='show'>
<div class='ng-modal-overlay' ng-click='hideModal()'></div>
<div class='ng-modal-dialog' ng-style='dialogStyle'>
<p>Select New Customer</p>
</div>
</div>
</div>
单击并打开模式的按钮:
<button ng-click='selectCustomer = true;'>SELECT/CREATE CUSTOMER</button>
和我一般的 modal-dialog-select 指令 HTMl
<modal-dialog-select show='selectCustomer'></modal-dialog-select>
知道为什么 scope.show 没有在我的指令中更新吗?谢谢!
这可能是 javascript 按值传递的有趣问题,因为 var 是 bool。老实说,我不擅长解释这个问题的根本原因。
尝试将其作为对象传递
ng-click='selectCustomer.value = true'
那么您的表演范围 属性 将是 scope.show.value。
似乎工作正常...
function modalDialogSelect() {
return {
restrict: 'AE',
scope: {
show: '='
},
replace: true,
transclude: true,
link: function(scope, element, attrs) {
scope.dialogStyle = {};
scope.hideModal = function() {
scope.show = false;
};
},
template: "<div class='ng-modal' ng-show='show'><button ng-click='hideModal()'>Here's the Modal</button></div>"
};
}
angular.module('test', [])
.controller('testCtrl', function() {})
.directive('modalDialogSelect', modalDialogSelect);
<script src="https://code.angularjs.org/1.3.0/angular.js"></script>
<div ng-app="test">
<div ng-controller="testCtrl as vm">
<button ng-click='vm.selectCustomer = true;'>SELECT/CREATE CUSTOMER</button>
<modal-dialog-select show='vm.selectCustomer'></modal-dialog-select>
</div>
</div>
您在生成模态模板时遇到问题
Error: Template must have exactly one root element. was:
<div class='ng-modal' ng-show='show'>
<div class='ng-modal-overlay' ng-click='hideModal()'></div>
<div class='ng-modal-dialog' ng-style='dialogStyle'>
<p>Select New Customer</p>
</div>
</div>
</div>
删除最后一个 </div>
后一切正常。
var app = angular.module("myApp", []);
app.controller('ParentController', function($scope) {
$scope.selectCustomer = false;
});
app.directive('modalDialogSelect', function() {
return {
restrict: 'AE',
scope: {
show: '='
},
replace: true,
transclude: true,
link: function(scope, element, attrs) {
scope.dialogStyle = {};
scope.hideModal = function() {
scope.show = false;
};
},
templateUrl: "modal.html"
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.min.js"></script>
<div ng-app="myApp" ng-controller="ParentController">
<p>Flag is: {{selectCustomer}}</p>
<button ng-click='selectCustomer = !selectCustomer'>SELECT/CREATE CUSTOMER</button>
<modal-dialog-select show='selectCustomer'></modal-dialog-select>
<script type="text/ng-template" id="modal.html">
<div class='ng-modal' ng-show='show'>
<div class='ng-modal-overlay' ng-click='hideModal()'></div>
<div class='ng-modal-dialog' ng-style='dialogStyle'>
<p>Select New Customer</p>
</div>
</div>
</script>
</div>