文本字段的值将显示动态div

时间:2018-04-09 18:43:37

标签: javascript angularjs

我的网页有一个文本字段,当您输入0时,将禁用“添加”按钮。当输入超过0时,按钮应该启用,它应该在文本下方显示“hello”,输入值的次数。例如,输入2,单词“hello”应显示2次相同的3。

这是我的代码:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.name = "0";
  $scope.ash = "";
  var x = angular.element(document.getElementById("xyz"));
  $scope.hello = function() {
    $scope.name = x.val();
    $scope.ash = ['ashish'];
    for (var i = 0; i < $scope.name; i++) {
      alert($scope.ash);
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl" ng-init="$scope.name">
  <input id="xyz" type="text" value="" ng-model="name" />
  <button ng-click="hello()" ng-disabled="name == 0">Add</button>
  <p ng-model="abc" ng-repeat="x in $scope.ash">hello</p>
</div>

1 个答案:

答案 0 :(得分:1)

你可以这样做:

  1. id添加到button,以便能够在JS中找到它。

  2. 在您要添加hello之后找到元素:

    var button = angular.element(document.getElementById('button'));
    
  3. 在该元素hello次之后添加i

    button.after('<p>hello</p>');
    
  4. var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
        $scope.name = "0";
        $scope.ash = "";
        var x = angular.element(document.getElementById("xyz"));
        $scope.hello = function() {
            $scope.name = x.val();
            $scope.ash = ['ashish'];
            var button = angular.element(document.getElementById('button'));
            for (var i = 0; i < $scope.name; i++) {
                button.after('<p>hello</p>');
            }
        }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="myApp" ng-controller="myCtrl" ng-init="$scope.name">
      <input  id="xyz" type="text" value="" ng-model="name"/>
      <button ng-click="hello()"  ng-disabled="name == 0" id="button">Add</button>
    </div>