angularjs值未从文本框

时间:2015-12-07 16:46:06

标签: javascript angularjs angularjs-directive angularjs-scope angularjs-model

我有这个HTML标记:

<div data-ng-controller="VehicleProfileController">

<modal title="Add Vehicle Info" visible="showVehicleInfo">
    <div class="container">
        <div class="col-xs-4">
            <div class="row">
                Title <input type="text" ng-model="NewVMPTitle" class="form-control"/>
                Miles <input type="text" ng-model="NewVMPMiles" class="form-control"/>
            </div>
            <div class="row">
                <button ng-click="addVehicleData()" class="btn btn-success pull-right">Save</button>
            </div>
        </div>
    </div>
</modal>

</div>

然后在控制器中我有这个:

$scope.addVehicleData = function () {
            alert($scope.NewVMPTitle + ' ' + $scope.NewVMPMiles);
        };

NEWVMPTitle和NewVPMMiles都是空的,我错过了什么吗?

谢谢,Laziale

1 个答案:

答案 0 :(得分:3)

我认为最好使用ng-model将参数解析为控制器,而不是使用范围从ng-model中捕获值

Title <input type="text" ng-model="item.title" class="form-control"/>
Miles <input type="text" ng-model="item.miles" class="form-control"/>

<button ng-click="addVehicleData(item)" class="btn btn-success pull-right">Save</button>

这是针对js的

$scope.addVehicleData = function (item) {
    alert(item.title + ' ' + item.miles);
};