从angular js将值传递给javascript函数

时间:2016-12-31 05:50:15

标签: javascript angularjs

我想将一个值从Angular传递给Javascript函数。用于呈现元素的角度代码是:

<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

知道我错过了什么吗?感谢

2 个答案:

答案 0 :(得分:1)

尝试不带表达

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

<强>样本

&#13;
&#13;
var app = angular.module('DemoApp', [])
app.controller('VCController', function($scope) {
  
 var t = $scope;
 t.patient ={id:1};
  t.chatNow = function(k) {
         console.log(k + "--");
 };
 
});
&#13;
<!DOCTYPE html>
<html>
<head>
  <script data-require="angular.js@1.4.7" 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>
&#13;
&#13;
&#13;

答案 1 :(得分: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>