简体   繁体   English

angular指令不会使用控制器更新双向绑定

[英]angular directive won't update two-way binding with controller

I have a modal directive that should become visible when a button is pressed. 我有一个模式指令,当按下按钮时,该指令应该可见。 however, the value responsible for determining whether the modal should be open that is two-way bound with the directive does not seem to update. 但是,与指令双向绑定的负责确定是否应该打开模式的值似乎没有更新。 any ideas? 有任何想法吗?

here is the directive JS: 这是指令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"
  };
 }

directive Inner HTML URL: 指令内部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>

the button to click and open the modal: 单击按钮以打开模式:

<button ng-click='selectCustomer = true;'>SELECT/CREATE CUSTOMER</button>

and the modal-dialog-select directive in my general HTMl 和我的一般HTMl中的modal-dialog-select指令

<modal-dialog-select show='selectCustomer'></modal-dialog-select>

any ideas why scope.show isn't updating in my directive? 为什么我的指令中的scope.show没有更新的任何想法? Thanks! 谢谢!

You had a problem in your modal's template that was producing 您在模态模板中产生了一个问题

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>

After removing the last </div> everything works. 删除最后一个</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> 

This could be that fun issue of javascript passing by value since the the var is a bool. 这可能是javascript按值传递的有趣问题,因为var是布尔值。 Honestly, I'm bad at explaining the root cause on this one. 老实说,我不善于解释这一问题的根本原因。

Try passing it as an object 尝试将其作为对象传递

ng-click='selectCustomer.value = true'

Then your show scope property would be scope.show.value. 然后,您的show scope属性将是scope.show.value。

Seems to work fine... 似乎工作正常...

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM