【问题标题】:Model binding not same in Chrome and FirefoxChrome 和 Firefox 中的模型绑定不同
【发布时间】:2015-09-15 16:36:08
【问题描述】:

我遇到了奇怪的情况,相同的代码在 chrome 和 firefox 中没有按预期工作。

这是html代码:

<div class="modal" id="FeeModal" role="dialog" aria-hidden="true" data-backdrop="static">
   <div class="modal-dialog">
      <div class="modal-body">
        <label>Fee Paid<label>
        <input id="FeePaid" ng-model="feeModel.FeePaid" />
      </div>
      <div class="modal-footer">
         <button ng-click="feeModelUpdate()">Apply</button>
      </div>
   </div>
</div>

这是控制器中的代码:

//this is the method I call to open modal
$scope.addFee = function(section, index, isRecordingFee) {
    var data = { 
        section = section,
        index = index,
        isRecordingFee = isRecordingFee
    }

    $scope.feeModel = data;

    $(".popover").hide();
    $("#FeeModal").modal("show");
}

$scope.feeModelUpdate = function() {

    // in this method I have problem
    $scope.feeModel.FeePaid

}

在我的方法 feeModelUpdate 中,我想从 FeePaid 访问值,但我在不同的浏览器上遇到问题。

在 chrome 上,此代码有效,我的 FeePaid 中有价值,但在 Firefox 上无效。

所以我认为这是在不同浏览器中范围界定的某种问题?

如果我将我的 html 代码更改为:

ng-model="$parent.feeModel.FeePaid"

然后突然之间,这在 Firefox 中可以正常工作,但在 Chrome 中却无法正常工作。所以我需要帮助来解决这个问题。我不确定出了什么问题,但奇怪的是这段代码在一个浏览器中工作而在另一个浏览器中不工作,反之亦然。

【问题讨论】:

  • 首先你说的方法有问题,其实不是正确的方法。将其更改为 $scope.feeModelUpdate = function() { $scope.feeModel.FeePaid //do here what you need }
  • 感谢您在问题代码中指出我的错误。那是来自我的控制器的代码,我没有提出问题。
  • 还有一件事,我遇到了这样一种情况,即代码在 Chrome 中无法运行,并且正在疯狂地谷歌搜索,但什么也没有。我刚刚在 Chrome 中禁用了所有扩展,并且代码运行良好。使用您拥有的代码进行检查。您还看到任何控制台错误或其他什么吗?

标签: html angularjs google-chrome firefox angularjs-ng-model


【解决方案1】:

我通过更改控制器代码解决了这个问题。我在控制器启动时初始化了feeModel

$scope.feeModel = {};

...

$scope.addFee = function(section, index, isRecordingFee) {

...

它解决了问题,但我不确定为什么它解决了问题。如果有人有一些解释,如果他能发布它会很棒。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-27
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    相关资源
    最近更新 更多