angular direction won't更新与控制器的双向绑定
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"
};
}
指令内部HTML URL:
<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>
以及我的通用HTMl 中的模式对话框选择指令
<modal-dialog-select show='selectCustomer'></modal-dialog-select>
你知道scope.show为什么没有在我的指令中更新吗?谢谢
您的模态模板在生成时出现问题
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>
这可能是javascript传递值的有趣问题,因为var是一个bool。老实说,我不善于解释这件事的根本原因。
尝试将其作为对象传递
ng-click='selectCustomer.value = true'
那么您的show scope属性将是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>
相关文章:
- ng绑定和ng href问题.ng href未从控制器加载数据
- 将指令绑定到控制器属性
- 当显式定义控制器参数时,默认模型绑定器会发生异常
- AngularJS 1.5.x服务未正确绑定,并且未在控制器中更新
- 将角度材质设计复选框绑定到控制器中的数组
- 在将绑定应用于控制器之后,是否会发出Angular$scope事件
- Ember.js:将Em.$.getJSON转换为promise并将响应绑定到控制器上下文的正确方法
- 同一页面上两个不同控制器的Angularjs双向绑定字段
- 角度控制器不绑定服务对象属性 ng-repeat
- 在 AngularJS 中将数据从视图绑定到控制器
- 工厂和控制器之间的“绑定”
- 无法绑定到没有指令 + angularjs 标识符的控制器
- AngularJS路由未绑定控制器
- AngularJS隔离的作用域和控制器绑定
- 角度 JS - 数据未从控制器绑定到视图
- 我想要从控制器绑定iframe的src
- Ember.js视图值到控制器绑定
- Ember.js中使用{{render}} helper的嵌套控制器绑定
- 通过指令将元素添加到 DOM 并从控制器绑定范围监视(角度.js)
- 无法将更新的控制器绑定为变量以在 AngularJS 中查看