【问题标题】:2-way data binding not work for me2路数据绑定对我不起作用
【发布时间】:2017-09-20 10:52:03
【问题描述】:

在 html 文件中,输入字段的值已更新。并且使用 $scope,我第一次能够分配输入字段。但是当我在 htlm 中更改输入字段时,输入字段值会更改,但在 js 中,控制台日志值不会更改。我想在 js 函数中获取更新的值。我正在使用 laravel 框架。实际上它可以工作。但我的项目它不起作用。谢谢。

index.html

<div class="form-group">
<div class="col-sm-12">
    <input type="text" class="form-control"
           id="send_amount"
           name="send_amount"
           ng-model="send_amount"
           ng-keyup="calculateReceivedAmount()" required>
</div>
@{{ send_amount }}

还有我的js文件

var exchange = angular.module('app', []);
exchange.controller('MoneyExchangeController', MoneyExchangeController);
function MoneyExchangeController($scope, $http) {
    $scope.send_amount = 100;
    $scope.calculateReceivedAmount = function () {
        console.log($scope.send_amount);
    }
}

【问题讨论】:

  • 我在小提琴上试过你的代码,它看起来适合我

标签: html angularjs laravel-5


【解决方案1】:

AngularJS 绑定非对象变量有问题,尝试如下绑定你的输入:

index.html

<input type="text"
       class="form-control"
       id="send_amount"
       name="send_amount"
       ng-model="send_amount.value"
       ng-keyup="calculateReceivedAmount()"
       required />

app.js

var exchange = angular.module('app', []);
exchange.controller('MoneyExchangeController', MoneyExchangeController);
function MoneyExchangeController($scope, $http) {
    $scope.send_amount = {
        value: 100
    };
    $scope.calculateReceivedAmount = function () {
        console.log($scope.send_amount.value);
    };
}    

【讨论】:

    猜你喜欢
    • 2018-01-11
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    相关资源
    最近更新 更多