【问题标题】:ngDialog scope variables not getting updatedngDialog 范围变量未更新
【发布时间】:2015-06-17 00:57:40
【问题描述】:

我是 AngularJs 和 ngDialog 的新手,我无法让我的绑定在 ngDialog 模式和我的控制器之间工作。我通过指定 { scope: $scope } 将控制器的作用域注入到模态中,并且我可以访问控制器中定义的方法,但是控制器中定义的模型绑定无法正常运行。

我正在尝试使用模式来允许用户向组织添加地址。

这里是 main.js

angular.module('wizardApp')
  .controller('MainCtrl', ['$scope', 'ngDialog', MainCtrl]);

function MainCtrl($scope, ngDialog) {
  $scope.hide_wizard_button = false;
  $scope.open_wizard = function () {
    $scope.hide_wizard_button = true;
    ngDialog.open({
      template: 'wizard',
      controller: 'wizardCtrl',
      scope: $scope
   })
 }
}

angular.module('wizardApp')
    .controller('wizardCtrl', ['$scope', wizardCtrl]);
function wizardCtrl($scope){
  $scope.step = 1;
  $scope.name = null;
  $scope.phone_number = null;
  $scope.email_address = null;
  $scope.password = null;
  $scope.step_forward = function(){
    if ($scope.step === 1){
      if(validate_name($scope.name) &&     validate_phone_number($scope.phone_number) && validate_address($scope.address)){
        $scope.step++;
      }
    }
    if ($scope.step == 2){
       if(validate_email($scope.email_address) &&   validate_password($scope.password)){
        $scope.step++;
      }
    }
  };

以下是我的 ng 模板:

<script type="text/ng-template" id="wizard">
  <form id="msform">
    <!-- progressbar -->
    <ul id="progressbar">
      <li ng-class="{true: 'active'}[step==1]">Personal Details</li>
      <li ng-class="{true: 'active'}[step==2]">Social Profiles</li>
      <li ng-class="{true: 'active'}[step==3]">Personal Details</li>
    </ul>
    <!-- fieldsets -->
    <fieldset ng-if="step == 1">
      <h2 class="fs-title">Enter Your personal Details</h2>

      <h3 class="fs-subtitle">This is step 1</h3>
      <input type="text" placeholder="Name" ng-model="name"/>
      <input type="text" placeholder="Phone Number" ng-model="phone_number"/>
      <input type="text" placeholder="Address" ng-model="address"/>
      <input type="button" class="next action-button" value="Next" ng-click="step_forward()"/>
    </fieldset>
    <fieldset ng-if="step == 2">
      <h2 class="fs-title">Email Verification</h2>

      <h3 class="fs-subtitle">Your Email Address and password</h3>
      <input type="text" name="email" placeholder="Email Address"/>
      <input type="password" name="password" placeholder="Password"/>
      <input type="button" name="previous" class="previous action-button" value="Previous" ng-click="step_back()"/>
      <input type="button" name="next" class="next action-button" value="Next" ng-click="step_forward()"/>
    </fieldset>
    <fieldset ng-if="step == 3">
      <h2 class="fs-title">Thank You for signing up!</h2>
    </fieldset>
  </form>
</script>

错误是无法读取 null 的属性,这意味着 $scope.name 没有得到更新。

【问题讨论】:

  • 在哪一行出现“无法读取 null 属性”错误?您将名称设置为 null,它从哪里获取新值(来自用户输入)?如果新值来自用户输入,错误是在用户开始输入之前还是之后出现?
  • 当我在做$scope.name.length 时,validate_name 方法中出现错误,并且在单击下一个按钮并且用户已经在文本框中输入了名称时发生。

标签: javascript angularjs angularjs-scope


【解决方案1】:

看起来问题与模板中的 ng-if 指令有关。 ng-if 创建一个新的子作用域。

新作用域继承其父作用域的属性(通过原型继承)。当您将模型值存储在基元(字符串、数字、布尔值)而不是对象中时,就会出现类似的问题。

由于原型继承,子作用域中继承的字符串值会影响父作用域中的值。例如。你改变了孩子的价值,但父母没有注意到它。

这就是为什么 Angular 开发人员建议您“在模型中添加一个点”。将您的模型存储在一个对象中,该对象可以在原型继承中继续存在。

所以创建一个包含所有模型值的对象(在 HTML 中绑定):

$scope.step1Data = { name: null, phone: null }; // etc.

然后使用ng-model 绑定到它们(使用点!):

ng-model="step1Data.name"

请阅读this answer 中的精彩解释,了解血淋淋的细节。

【讨论】:

  • 亲爱的妈妈,谢谢你!我还没有阅读链接的文章(但我肯定会!)但它是否解释了如何禁用/防止子范围隐藏简单属性,或者我们应该总是使用“我们的模型中的点”?
  • @SelAromDotNet Angular 使用原型继承来制作子作用域,而属性的阴影正是原型继承在 Javascript 中的工作方式。禁用它的唯一方法是避免创建子范围(例如,您可以创建一个指令来显示 HTML 模板,而不是使用 ng-include 来显示相同​​的模板)。
  • 是的,昨晚通读了这篇文章,很棒(如果令人困惑)的东西!再次感谢,我觉得在“还有一个人偶然发现这个”名单上很愚蠢,但很高兴我终于明白了(有点):)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多