【问题标题】:Why can`t i use $scope.feedback.myChannel inside my controller if i can use it in my html page?如果我可以在我的 html 页面中使用它,为什么我不能在我的控制器中使用 $scope.feedback.myChannel?
【发布时间】:2016-07-20 22:27:45
【问题描述】:

我有一个使用 angularJs 的表单,所有代码都可以在问题末尾的我的 plunker 上找到。

在我的控制器里面我有

$scope.showTelfield= false; $scope.showEmailfield= false; 

在我的表单中以及我正在使用的电话字段和电子邮件字段中:

ng-show="showTelField"  ng-show="showEmailField"

如果用户从控制器内部设计的选择选项中选择它,则这些值应该为真。

在html中

<select class="form-control c-select" name="select" ng-model="feedback.myChannel" ng-options="channel.value as channel.label for channel in channels">
 <option value="">Select a method</option>
 </select>

在控制器中:

 $scope.feedback = {
  myChannel: "",
  firstName: '',
  lastName: '',
  agree: false,
  email: ''
};
$scope.channels = [{
  value: "Tel",
  label: "Tel"
}, {
  value: "Email",
  label: "Email"
}, {
  value: "Tel & Email",
  label: "Tel & Email"
}];


//Telling browser to view telephone or Email feild if needed..
    if($scope.feedback.myChannel === "Tel" || $scope.feedback.myChannel === "Tel & Email"){
        $scope.showTelField = true }

    if($scope.feedback.myChannel === "Email" || $scope.feedback.myChannel === "Tel & Email"){
        $scope.showEmailField = true }

$scope.feedback.myChannel 的值会根据用户的选择从空字符串更改为“Tel”、“Email”或“Tel & Email”,我在一个额外的 div 中显示它仅用于开发目的以确保它们随着用户的选择而改变。

但所有 if 语句都不起作用,尽管 $scope.feedback.myChannel 的值会根据我为开发而制作的显示 div 发生变化。 知道为什么吗?

你可以在我的 plunker 中看到我的代码 Here is my plunker

这是页面的截图

【问题讨论】:

    标签: angularjs angularjs-forms


    【解决方案1】:

    首先,您在控制器中拥有的ifs 只会在页面加载时被调用,之后angular 会忽略它除非您将您的逻辑放在@ 上的function 中987654328@.

    要检测&lt;select&gt; 的变化,您应该使用ngChange 指令,如下所示:

    查看:

    <select ng-change="change()" class="form-control c-select" name="select" ng-model="feedback.myChannel" ng-options="channel.value as channel.label for channel in channels">
    

    JS:

    $scope.change = function() {
      console.log($scope.feedback.myChannel);
    }
    

    然后,您尝试用来操作数据的那些变量(showTelField、showEmailField)可以简单地替换为指令,即 ngSwitch,这样您就可以拥有像这样:

    <div ng-switch="feedback.myChannel">
      <div class="form-group " ng-class="{'has-error': !feedbackForm.telnum.$pristine && feedback.tel.number == '' || feedback.tel.areacode == ''}" ng-switch-when="Tel">
      ...
      </div>
      <div class="form-group has-feedback" ng-class="{'has-error': !feedbackForm.email.$pristine && feedback.email == '' || feedbackForm.email.$invalid }" ng-switch-when="Email">
      ...
      </div>
      <div ng-switch-when="Tel & Email">
      ...
      </div>
    </div>
    

    另外,我建议您在 code 的某些部分使用 ngIf 指令而不是 ngShow 作为一种良好做法。

    看看分叉的DEMO。

    【讨论】:

    • 谢谢 .. 请注意:ng-show="feedback.agree && (feedback.myChannel== 'Tel' || feedback.myChannel == 'Tel & Email') "> 也作品!是吗?我可以通过几个解决方案达到想要的结果,但我的问题是我如何能够在 html 中使用 feedback.myChannel 并且不能在控制器中将其用作 $scope.feedback.myChannel ?当你说:没有办法 showTelField 和 showEmailField 为真,因为你没有将它们设置为真..但是当你选择一个选项时,你将 feedback.myChannel 设置为一个值..请去你的 plunker 并使用 ng-如上所示。
    • 并且没有所有这些,查看右侧显示的 div 它显示 feedback.myChannel 在选择时采用了一个值..这就是我的 if 语句所说的.. 将 showTelField 和 showEmailField 更改为如果 feedback.myChannel 有一个值,则为真。 !!!
    • 好吧,我不确定它是否有效。我什至没有测试,因为ng-show 是一种不好的做法,您应该使用ngIf 或BETTER @ 987654342@ 正是为这种事情而制作的。对于您的问题,答案是:否,这些值 (showTelField & showEmailField) 不可能是真的,因为您没有办法让它们成为真的......上面的解决方案有问题吗?当你说“我不能在我的控制器中使用变量”时,你是什么意思????
    • 我正在寻找答案而不是解决方案。非常感谢您的努力.. :))
    • 所以解决方案不是答案?
    【解决方案2】:

    最好信任 FormControl 对象,将ng-show="feedback.agree" 更改为ng-if="feedback.agree" 并设置选择字段required:

    <div class="col-sm-3 col-sm-offset-1" ng-if="feedback.agree">
        <select class="form-control c-select" name="select" ng-model="feedback.myChannel" ng-options="channel.value as channel.label for channel in channels" required>
            <option value="">Select a method</option>
        </select>
        <span class="help-block" ng-show="invalidChannelSelection && feedback.myChannel == ''">Select a method</span>
    </div>
    

    然后您可以根据表单的有效性状态禁用提交按钮(我从ngDisabled 指令中删除了|| invalidChannelSelection):

    <button type="submit" class="btn btn-primary" name="submit" ng-disabled="feedbackForm.$invalid">Send Feedback</button>
    

    工作示例:http://plnkr.co/edit/un9BYMa81cqWOCyd4Vdj?p=preview

    【讨论】:

    • 谢谢,我查看了你的 plunker,但在选择选项时它没有显示这些字段。
    • 如果用户不想被联系,我想我不能要求选择,所以表格总是无效的..真的!!另请阅读我在下面的评论@developer33 ..因为我正在解释更多关于我的问题。
    猜你喜欢
    • 2016-11-21
    • 1970-01-01
    • 2017-06-19
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 2013-05-26
    • 1970-01-01
    相关资源
    最近更新 更多