javascript - Angular JS 使用 url 参数预填充表单

标签 javascript angularjs

我有以下 Angular Controller :

'use strict';
angular.module("generatorApp").controller('demogController',['$scope','$location','$http',function($scope, $location, $http){
        $scope.submit=function(){
        var feet=$scope.feet;
        var inches=$scope.inches;
        var isMale = false;

        if($scope.gender == 'male'){
            isMale = true; 
        }else{
            isMale = false;
        };

        var data = {
            emailId:$scope.emailId,
            gender:isMale,      
            heightCm: 2.54 *(this.feet * 12 + this.inches)      
        };

        localStorage.setItem('demogData', JSON.stringify(data));

    };  
}]);

现在这是 html:

<table>
<tr>
    <th> feet </th>
    <th> inches </th>
</tr>
<tr>
    <td><input type="number" ng-model="feet" placeholder="ft" name="feet" id="feet"></td>
    <td><input type="number" ng-model="inches" placeholder="in" name="inches" id="inches"><td>
</tr>   
</table>

电子邮件和其他一些条目依此类推。 我正在尝试使用以下格式的 url 字符串来预填充这些表条目:

https://blahblah.com/form?feet=x&inches=y 

因此,如果输入以下字符串,则已经填充了 by 表单。任何建议,将不胜感激。

最佳答案

您可以通过以下方式获取查询字符串参数:

$scope.feet = $location.search().feet; 

通常建议您用于 ng-model 的模型是对象的属性。例如:

$scope.formModel = {
    feet: $locaion.search().feet,
    inches: $location.search().inches
};

然后这样引用它们:

<tr>
<td><input type="number" ng-model="formModel.feet" placeholder="ft" name="feet" id="feet"></td>
<td><input type="number" ng-model="formModel.inches" placeholder="in" name="inches" id="inches"><td>
</tr> 

关于javascript - Angular JS 使用 url 参数预填充表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38959451/

相关文章:

javascript - 使用 JQuery 设置包含 A href 值的*所有* IMG src 值

javascript - 使用 vanilla JavaScript 按钮的电子邮件表单

angularjs - Angular ng-options 按问题跟踪

javascript - Google map - 拖放标记并绘制路线

javascript - 检查嵌套的javascript对象数组中是否存在元素

javascript - Bootstrap-datetimepicker 选择过去几个月

javascript - jQuery:用于延迟 removeClass() 的脚本延迟时间太长

javascript - 使用 Selenium 脚本的 Javascript Executor 在网页上水平滚动

angularjs - i18next - 语言更改时字符串不会更新

javascript - AngularJS 中算术运算符的问题