【问题标题】:How to bind one angular model to the value of another ng-model如何将一个角度模型绑定到另一个 ng-model 的值
【发布时间】:2020-02-29 15:28:10
【问题描述】:

我有一个像这样被选中的单选按钮:

<input type="radio" ng-model="lesson.sectionID" value="{{section.$id}}">

我想将该输入的值绑定到另一个模型,我尝试了以下方法:

<input type="text" ng-model="module.sectionID" ng-bind="lesson.sectionID">

<input type="text" ng-model="module.sectionID" ng-value="lesson.sectionID">

当我尝试 ng-value 时,它​​会将文本输入设置为正确的值,但未设置模型的实际值。

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    您可以使用分配模型

    ng-model="module.sectionID"
    ng-value="module.sectionID=lesson.sectionID" 
    

    【讨论】:

      【解决方案2】:

      您可能正在寻找此解决方案

      angular.module('choices', [])
        .controller("MainCtrl", ['$scope', function($scope) {
      
          $scope.color = '';
      
          $scope.colors = [
            "Red",
            "Green",
            "Blue",
            ""
           ];
      
          $scope.changeColor = function(){
            $scope.color = "Red"
          };
      
      }]);
      
      
      
      <html>
      <head>
      <body ng-app="choices" ng-controller="MainCtrl">
        <div ng-repeat="color in colors">
           <input type="radio" ng-model="$parent.color" ng-value="color" id="{{color}}" name="color">
             <label >
               {{color || 'Other'}}
             </label>
            <input type="text" ng-model="$parent.color" ng-show="color==''">
        </div>
        <p></p>
        The chosen color is <strong>{{color}}</strong>
        <p></p>
        <button ng-click="changeColor()">Change color</button>
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 2014-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-03
        • 2014-10-24
        • 2016-02-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多