angular指令不会更新与控制器

时间:2015-06-30 18:31:58

标签: javascript angularjs angular-directive

我有一个模态指令,当按下按钮时该指令应该可见。但是,负责确定模态是否应该打开的值与指令双向绑定似乎不会更新。任何想法?

这是指令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网址:

<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指令

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

为什么scope.show没有在我的指令中更新?谢谢!

3 个答案:

答案 0 :(得分:3)

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.

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>

答案 1 :(得分:0)

这可能是一个有趣的javascript传递值的问题,因为var是一个bool。老实说,我不善于解释这个问题的根本原因。

尝试将其作为对象传递

ng-click='selectCustomer.value = true'

然后你的show scope属性将是scope.show.value。

答案 2 :(得分:0)

似乎工作得很好......

&#13;
&#13;
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);
&#13;
<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>
&#13;
&#13;
&#13;