2016-12-31 40 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> 
相关问题