Saya memiliki formulir sederhana di angularjs, yang memasukkan nilai a dan nilai b. ketika saya mengklik tombol, saya ingin nilai-nilainya diperingatkan. Bagaimana aku melakukan itu?

<div ng-controller="myController">
    <p><label>value a : </label><input type="text" ng-model="valuea" name="valuea" id="valuea" /></p>
    <p><label>value b  : </label><input name="valueb" id="valueb" ng-model="valueb"/></p>

    <button type="button" ng-click = "add()"  >Sign In</button> 
</div>

<script>
   angular.module('myApp', [])
   .controller('myController', ['$scope', function($scope) {
   function myController($scope) {
       $scope.add = function(){
           alert("valuea:"+$scope.valuea);
           alert("valueb:"+$scope.valueb);
       } 
   };
}]);
0
user4752891 2 Juni 2019, 10:09

2 jawaban

Jawaban Terbaik

Inilah jawabannya:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myController">
      <p>
        <label>value a : </label><input type="text" ng-model="valuea" name="valuea" id="valuea" />
      </p>
      <p>
        <label>value b : </label><input name="valueb" id="valueb" ng-model="valueb" />
      </p>

      <button type="button" ng-click="add()">Sign In</button>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myController', function($scope) {
  $scope.add = function() {
    alert("valuea:" + $scope.valuea);
    alert("valueb:" + $scope.valueb);
  }
});
</script>

</body>
</html>
0
Farzan Najipour 2 Juni 2019, 07:47

Ada masalah tertentu dengan kode Anda. Di html Anda tidak ada modul myApp. Juga dalam callback pengontrol Anda, tidak perlu menambahkan fungsi terpisah dengan nama pengontrol function myController() {}.

angular.module('myApp', [])
   .controller('myController', ['$scope', function($scope) {
       $scope.add = function(){
           alert("valuea: "+$scope.valuea);
           alert("valueb: "+$scope.valueb);
       } 
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="myController">
      <p><label>value a : </label><input type="text" ng-model="valuea" name="valuea" id="valuea" /></p>
      <p><label>value b  : </label><input name="valueb" id="valueb" ng-model="valueb"/></p>
      <button type="button" ng-click = "add()"  >Sign In</button>
  </div>
</div>

Lihat biola yang berfungsi - https://jsfiddle.net/otqzk6ua/

1
random 2 Juni 2019, 07:33