Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何基于选择框和反之亦然填充输入字段a_Javascript_Html_Angularjs - Fatal编程技术网

Javascript 如何基于选择框和反之亦然填充输入字段a

Javascript 如何基于选择框和反之亦然填充输入字段a,javascript,html,angularjs,Javascript,Html,Angularjs,我正在尝试填充一个选择框和一个输入字段 意思是 每当有人试图选择一个国家时,我在输入框中设置相关的货币,并在输出中显示国家+货币 每当有人试图输入货币时,我在选项框中设置国家,并在输出中显示国家+货币 我的控制器: app.controller('MainCtrl', function ($scope) { $scope.$watch('countrySelect', function () { for ($i = 0; $i < arr.le

我正在尝试填充一个选择框和一个输入字段

意思是

  • 每当有人试图选择一个国家时,我在输入框中设置相关的货币,并在输出中显示国家+货币
  • 每当有人试图输入货币时,我在选项框中设置国家,并在输出中显示国家+货币
  • 我的控制器:

    app.controller('MainCtrl', function ($scope) {           
        $scope.$watch('countrySelect', function () {
            for ($i = 0; $i < arr.length; $i++) {
                var temp = arr[$i]['select'];
                if (temp === $scope.countrySelect) {
                    $scope.outputCountry = arr[$i].country + arr[$i].currency;
                    $scope.countryCurrency = arr[$i].currency;
                }
            }
        });
    
        $scope.$watch('countryCurrency', function () {
            for ($i = 0; $i < arr.length; $i++) {
                var temp = arr[$i]['currency'];
                if (temp === $scope.countryCurrency) {
                    $scope.outputCountry = arr[$i].country + $scope.countryCurrency;
                    $scope.countrySelect = arr[$i].country;
                }
            }
        });
    });
    
    app.controller('MainCtrl',函数($scope){
    $scope.$watch('countrySelect',函数(){
    对于($i=0;$i
    相应部门:

    <div ng-controller="MainCtrl">
        <form action="#">
            <select ng-model="countrySelect" ng-init="countrySelect = 11">
                <option value="11">US</option>
                <option value="12">UK</option>
            </select>
            <input ng-model="countryCurrency" />
            <input ng-model="outputCountry" />
        </form>
    </div>
    
    
    美国
    英国
    
    我想出了一个好主意

  • 如果不使用
    $scope.watch
    并多次启动它,有没有更好的方法实现同样的效果
  • 在加载期间(默认情况下选择任何国家/地区之前),
    $scope.watch
    不会启动,并且根本不会填充值
  • 这里有什么指示吗?

    
    选择您喜爱的水果:
    苹果
    橙色
    菠萝
    香蕉
    函数myFunction(){
    var x=document.getElementById(“mySelect”).value;
    document.getElementById(“TextBox1”).value=x;
    }
    
    在第一个输入字段中填写“usd”或“sek”,或在选择框中选择国家/地区

    角度模块(“应用程序”,[]) .controller('MainCtrl',函数($scope) { $scope.countryCurrency=“”; $scope.outputCountry=“”; $scope.countries=[ { 名称:“瑞典”, 货币:“瑞典克朗” }, { 名称:“美国”, 货币:“美元” } ]; $scope.selected={ 国家:$scope.countries[0]。名称 }; $scope.$watch(“selected.country”,function()) { var country=$scope.countries.find(函数(国家) { 返回country.name==$scope.selected.country; }); 国际单项体育联合会(国家) { $scope.outputCountry=country.name+country.currency; $scope.countryCurrency=country.currency; } 其他的 { $scope.outputCountry=“”; $scope.countryCurrency=“”; } }); $scope.$watch(“国家货币”,函数() { var country=$scope.countries.find(函数(国家) { return country.currency==$scope.countryCurrency.toUpperCase().trim(); }); 国际单项体育联合会(国家) { $scope.outputCountry=country.name+$scope.countryCurrency; $scope.selected.country=country.name; } 其他的 { $scope.outputCountry=“”; } }); });
    
    {{country.name}
    
    这正是我想要的。谢谢,亲爱的!没有问题:)有一个
    ng change=countryChanged()
    函数。在哪里使用它?在任何地方,都不能在$scope函数或$scope上使用ng change。$watch,它们给出的结果几乎相同。我一定是先想到使用ng change,然后改变主意使用$watch:P我相信还有更干净的方法来写这个,有点匆忙:)编辑以删除ng change。