【问题标题】:Change angular $scope value when different radio button is clicked单击不同的单选按钮时更改角度 $scope 值
【发布时间】:2017-07-22 09:54:31
【问题描述】:

我正在做一个角度项目,我试图通过更改单选按钮来更改范围值和元素值。单击单选按钮 1 时,我从服务器获取值并将其保存在范围和元素中。当我单击单选按钮 2 时,必须删除所有范围和元素。清空所有范围。但是我的问题是当我编辑输入并单击单选按钮 2 时,会显示输入中的跨度值和占位符。不得显示跨度。这是我的代码。

index.html

<div class="row">
    <div class="col" style="border: none;">
      <ion-item class="item-checkbox" style="border: none;">
        <label class="checkbox">
<input type="radio" id="oldChecked" ng-value="true" ng-model="oldChecked" ng-click="showName()">
            </label> Radio 1
      </ion-item>
    </div>
    <div class="col" style="border: none;">

      <ion-item class="item-checkbox" style="border: none;">
        <label class="checkbox">
                <input type="radio" id="newChecked" ng-value="true" ng-model="newChecked" ng-click="deleteName()">
            </label> Radio 2
      </ion-item>

    </div>
  </div>
   <div class="col" style="width: 100%;">
  <label class="item item-input item-floating-label" style="
  border-left:  none;border-right: none;border-top:none;">
  <span class="input-label" style="text-align: left;">Name 
  <font style="color: #ef473a;font-size: 14px;">*</font>
  </span>
  <input type="text" placeholder="Name" ng-model="pName"
   id="pName" name="pName" required>
 </label>
        </div>

controller.js

$scope.showName=function(){
$http.post(WebServer_URL, data, config)
  .success(function (data, status, headers, config) {
    var x2js = new X2JS();
    var jsonOutput = x2js.xml_str2json(data);
    var myPatient = JSON.parse(jsonOutput.string);
    $scope.pName = myPatient[0].Pt_Name;
    document.getElementById('pName').value = myPatient[0].Pt_Name;
}).error(function (data, status, header, config) {
    console.log("Status: " + status);
  });
}

    $scope.deleteName=function(){
    $scope.pName="";
    document.getElementById('pName').value = "";
 }

Here is the image showing span and placeholder

请在这个问题上帮助我。提前谢谢各位。

【问题讨论】:

  • 请同时添加 Span html 代码
  • 我无法为您服务,请您解释一下。谢谢
  • 我的意思是你的 Html 代码没有完全粘贴。
  • 谢谢,我已经更新了问题。
  • 您是否尝试过我已经回答的以下过程?

标签: javascript html angularjs angularjs-scope


【解决方案1】:
// radio buttons should have same ng-model
<input type="radio" value="oldChecked" ng-model="myOption" ng-click="showName()">
<input type="radio" value="newChecked" ng-model="myOption" ng-click="deleteName()">
<input type="text" id="pName" ng-model="pName" placeholder="Name">

  <script type="text/javascript">
    var app=angular.module("test",[]);
    app.controller("testCtrl",function($scope){
    $scope.showName=function(){
      $scope.pName = "Hello";
    }
    $scope.deleteName=function(){
     $scope.pName="";
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 2020-01-21
    相关资源
    最近更新 更多