2016-09-15 20 views
1

這裏是我的html部分從那裏我嘗試輸入詳細..NG-點擊沒有觸發任何事件

<div ng-app="angularPHP"> 
<div ng-controller="mainpagecntl"> 
    <div id="customer" class="cuscontainer" hidden="hidden"> 
     <div> 
      <h3 class="modal-title" >Add customer</h3> 
      <i class="fa fa-times" id="clo" aria-hidden="true"></i> 
     </div> 
      <table> 
      <tr> 

       <div class="form-group" > 
       <td><label id="label" class="control-label">customer Name:</label> 
       <td><input type="text" class="form-control" ng-model="customer_name" /> 
       </div> 
      </tr> 
      <tr> 
      <div class="form-group"> 
       <td><label id="label" class="control-label">Address:</label></td> 
       <td><input type="text" class="form-control" ng-model="customer_add"/></td> 
      </div> 
      </tr> 
      </table> 
       <div class="madal-footer"> 
        <button class="btn btn-primary" ng-click="custadd()">ADD</button><br /> 

       </div> 
     </div> 
</div> 
</div> 

這裏是我的角的js部分..

var app = angular.module('angularPHP', []); 
    app.controller('mainpagecntl', function($http,$scope) 
    { 
     $scope.custadd= function() 
      { 
      data={ 
        cname :$scope.customer_name,        
         cadd: $scope.customer_add, 
        } 
        $http.post("../pos_system/Widgets/addcust.php?add",data).success(function(data) 
        { 
        }); 
        } 
     }); 

但是當我點擊按鈕什麼都沒有發生,我的ng-click函數沒有觸發。

+0

你用ng-app指令引導了角嗎?你在使用ng-controller指令嗎? – Amygdaloideum

+0

我使用ng-controller指令。@ DanielBornstrand –

+0

指定var用於數據對象。提供console.log或點擊功能提醒並檢查。評論剩餘代碼 – vbharath

回答

0

嘗試在<div id="customer" class="cuscontainer" hidden="hidden">加入ng-controller="myCtrl"這樣

<div id="customer" class="cuscontainer" hidden="hidden" ng-controller="myCtrl"> 
+0

我已經添加了ng-controller。@ Mohammad Shuaib –

0

確定的功能在不觸發呢?它可能正在觸發,但只是沒有發佈數據。 console.log直接在函數內部檢查

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

app.controller('mainpagecntl', function($http,$scope) { 

    var vm = this; 

    vm.custadd = function() { 

     console.log('test'); 

     var data = { cname :$scope.customer_name, cadd: $scope.customer_add}; 

     $http.post("../pos_system/Widgets/addcust.php?add",data) 
     .success(function(data) { 
     console.log(data); 
     }); 

    } 

} 

那麼你的NG-點擊看起來應該是這樣

ng-click='vm.custadd()' 

看看是否有什麼差別。

注:您的NG-控制器也應該像

ng-controller="mainpagecntl as vm" 

在下面的評論中指出。

+0

我認爲bharathv在上面的註釋中是正確的,你可能忘記爲你的數據對象聲明var。 – user2085143

+0

它不工作.. @ user2085143 –

+0

發佈您的控制器! – user2085143

0

下面是您的代碼的工作演示https://plnkr.co/edit/k7yApAavre66KFAiVlOp?p=preview,當點擊添加按鈕時它工作得很完美。儘管我已經從表中刪除了一些div標籤,因爲這是不好的做法,而Angular在其指令被使用時不會識別這些標籤。

var app = angular.module('angularPHP', []); 
app.controller('mainpagecntl', function($http, $scope) { 
    $scope.custadd = function() { 
     $scope.data = { 
      cname: $scope.customer_name, 
      cadd: $scope.customer_add, 
     } 
     // $http.post("../pos_system/Widgets/addcust.php?add", data).success(function(data) {}); 
    } 
});