繁体   English   中英

如何将选定的按钮值获取到输入文本框中以绑定值angularjs

[英]how to get selected button value into the input text box to bind values angularjs

如何将选定的按钮值输入文本框? 如果我在输入框中更改文本,则必须将该文本绑定到按钮值..我有一个用jquery解决的代码,但是如何用angularjs解决呢? JSBin编辑链接

AngularJS

 var app = angular.module('myapp', []); app.controller('MainCtrl', function($scope) { //Code goes here// }); 
 <!DOCTYPE html> <html ng-app="myapp"> <head> <script src="https://code.angularjs.org/1.4.8/angular.js"></script> </head> <body ng-controller="MainCtrl"> <div> <button>Feed</button> <button>the</button> <button>Input</button> </div> <input type="text" value="click a button"> </body> </html> 

用jQuery解决

 var actualButton; $( "button" ).click(function() { actualButton = $(this); var text = $( this ).text(); $( "input" ).val( text ); }); $("input").on('keyup', function(e){ if(actualButton === undefined) return; actualButton.text($(this).val()); }); 

您需要结合使用ngModel和ngClick的数据绑定技术。 也许是这样的:

 <script src="https://code.angularjs.org/1.4.8/angular.js"></script> <script>angular.module('demo', []);</script> <div ng-app="demo"> <div> <button ng-click="i = 1" ng-init="button1 = 'Feed'">{{ button1 }}</button> <button ng-click="i = 2" ng-init="button2 = 'the'">{{ button2 }}</button> <button ng-click="i = 3" ng-init="button3 = 'Input'">{{ button3 }}</button> </div> <input type="text" ng-model="this['button' + i]" placeholder="click a button"> </div> 

只需在按钮上添加ng-click函数,然后将$event对象传递给它即可。 这样您就可以访问触发事件的DOM。

但是我个人不会像尝试在控制器内部直接访问DOM那样使用此方法。

标记

<div>
  <button ng-click="buttonClick($event)">Feed</button>
  <button ng-click="buttonClick($event)">the</button>
  <button ng-click="buttonClick($event)">Input</button>
</div>
<input type="text" ng-model="inputValue" value="click a button">

控制者

var app = angular.module('myapp', []);

app.controller('MainCtrl', function($scope) {
    $scope.inputValue = 'click a button';
    $scope.buttonClick = function(event){
        console.log(event);
        $scope.inputValue = event.target.innerText;
    };
});

普伦克在这里

角度方法是您可以使用ng-repeat数组渲染按钮,并将那些按钮置于作用域自身内部。 并从click对象传递按钮名称,并将其分配给inputValue作用域变量。

标记

<div>
  <button ng-click="$parent.selected = $index" ng-repeat="button in buttons">{{button.text}}</button>
</div>
<input type="text" ng-model="buttons[selected || 0].text" value="click a button">

$scope.buttons = [{text: 'Feed', id: '1'}, {text: 'the', id: '2'}, {text: 'Input', id: '3'}];

角度方式Plunkr

您也可以检查PLUNKER DEMO LINK

二手ng-clickng-modelng-change

在HTML中:

<body ng-controller="MainCtrl">
    <h1>Hello Plunker!</h1>
  <div>
  <button type="button" ng-click="setText($event)">Feed</button>
  <button type="button" ng-click="setText($event)">the</button>
  <button type="button" ng-click="setText($event)">Input</button>
 </div>
 <input type="text" ng-model="textVal" ng-change="changeButtonText()">
</body>

和控制器:

  $scope.textVal = 'click a button';
  $scope.seletetedEvent = {};
  $scope.setText = function(element) {
    console.log(element);
    $scope.seletetedEvent = element;
     $scope.textVal = element.currentTarget.innerHTML;
  };
  $scope.changeButtonText = function(){
    $scope.seletetedEvent.currentTarget.innerHTML = $scope.textVal;
  };

实际上,您不需要执行任何操作,只需将相同的$ scope变量绑定到文本框和按钮,

视图:

 <input  ng-model="name" id="txtname" />
 <button class="button button-full button-light" >{{name}}</button>

这是工作中的PlunkerApp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM