2016-12-31 77 views
1

我想將一個值從Angular傳遞給一個Javascript函數。角碼呈現元素是:從角度傳遞一個值到javascript函數js

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

這正確返回按鈕的HTML作爲

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

然而,當我嘗試用這個參數去調用函數作爲

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

});

這使我對控制檯輸出作爲

{{patient.id}}--1 

任何想法,我缺少什麼?由於

+0

使用'chatNow(patient.id)' – Tushar

+0

@Tushar,我應該在哪裏改變?你能說明一下嗎?謝謝 – Satya

+0

@Tushar,謝謝一位男士。你搖滾! – 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

試試這個,控制檯將拋出2的ID:

<!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> 
相關問題