2016-12-31 18 views
1

AngularからJavascript関数に値を渡したいとします。要素をレンダリングする角度のコードは次のとおりです。角度jsからjavascript関数に値を渡します

<button class="btn btn-danger" ng-click="chatNow('{{patient.id}}');">Chat </button> 

私は

app.controller("VCController",function ($scope, $http,$window){ 
var t = $scope; 
    t.chatNow = function(k) { 
      console.log(k + "--"+$window.sessionStorage.getItem("docId")); 
     }; 

としてこのPARAMで関数を呼び出すしようとすると、これは正しく、しかし

<button class="btn btn-danger" ng-click="chatNow('patient2');">Chat </button> 

として、ボタンのHTMLを返します。 });

これは私

{{patient.id}}--1 

としてコンソールに出力私が行方不明です何の任意のアイデアを提供しますか?表現なしのおかげ

+0

'chatNow(patient.id)'を使用してください – Tushar

+0

@ Tushar、どこで変更しますか?あなたは説明できますか?おかげで – Satya

+0

@タシャール、ありがとう、トンの男。あなたは揺れる! – Satya

答えて

1

てみ

<button class="btn btn-danger" ng-click="chatNow(patient.id);">Chat </button> 

DEMO

var app = angular.module('DemoApp', []) 
 
app.controller('VCController', function($scope) { 
 
    
 
var t = $scope; 
 
t.patient ={id:1}; 
 
    t.chatNow = function(k) { 
 
     console.log(k + "--"); 
 
}; 
 
    
 
});
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <script data-require="[email protected]" data-semver="1.4.7" src="https://code.angularjs.org/1.4.7/angular.js"></script> 
 
</head> 
 
<body ng-app="DemoApp" ng-controller="VCController"> 
 
<button class="btn btn-danger" ng-click="chatNow(patient.id);">Chat </button> 
 
</body> 
 
</html>

0

はこれを試してみて、コンソールには、IDの2を投げます:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Hello World, AngularJS - ViralPatel.net</title> 
    <script type="text/javascript" 
     src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script> 
    <script> 
    angular.module('app',[]) 
    .controller("VCController",function ($scope, $http,$window){ 

     var t = $scope; 
     $scope.patient = {id: 2}; 
     t.chatNow = function(k) { 
      console.log(k + "--"+$window.sessionStorage.getItem("docId")); 

      }; 
    }); 
    </script> 
</head> 
<body> 

<div ng-app="app"> 
    <div ng-controller="VCController"> 
     <button class="btn btn-danger" ng-click="chatNow(patient.id);">Chat </button> 
    </div> 
</div> 


</body> 
</html> 
関連する問題