如何将一个角度模型绑定到另一个ng模型的值

时间:2015-11-30 18:13:08

标签: angularjs

我选择了一个单选按钮:

<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时,它将文本输入设置为正确的值,但未设置模型的实际值。

2 个答案:

答案 0 :(得分:1)

您可以使用

分配模型
ng-model="module.sectionID"
ng-value="module.sectionID=lesson.sectionID" 

答案 1 :(得分:0)

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

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>