功能完全打印

时间:2017-08-24 13:48:34

标签: javascript angularjs

AngularJs非常新,并试图找出我的功能完全打印出来的原因如下。

Hello function () { return (this.sal) * 12; }

这是我的代码。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    <script type="text/javascript">
    var app=angular.module("sample",[]);
    app.controller("emp",function($scope){
      $scope.Name="Jag";
      $scope.sal="4500"
      $scope.getAnnualSal = function()
      {
         return (this.sal) * 12;
      }
    });
    </script>
  </head>
  <body ng-app="sample">
<div ng-controller="emp">
  Hello {{getAnnualSal}}
</div>
  </body>
</html>

3 个答案:

答案 0 :(得分:3)

试试这个,它不是变量的函数:

@{
    ViewBag.Title = "Home Page";
}
@using WebApplication2.Models;
@model List<GelirGider>



<html>
<head>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">

        // Load the Visualization API and the corechart package.
        google.charts.load('current', { 'packages': ['corechart'] });

        // Set a callback to run when the Google Visualization API is loaded.
        google.charts.setOnLoadCallback(drawChart);

        // Callback that creates and populates a data table,
        // instantiates the pie chart, passes in the data and
        // draws it.
        function drawChart() {

            // Create the data table.
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Ay');
            data.addColumn('number', 'Gelir');
           

            $.ajax({
                url: '/Home/Index2',
                type: 'POST',
                dataType: 'json',
                success: function (data) {
                    alert('asdasda');
                    var data = new google.visualization.DataTable();
                    data.addColumn('string', 'Ay');
                    data.addColumn('number', 'Gelir');
                    
                    $.each(data, function (i, item) {
                        data.addRows([ item.Ay ,item.Gelir])
                        //data.addRows(eval('[' + item.Ay + ', ' + item.Gelir ']'));
//                        data.addRows([ ['Mushrooms', 3], ]);
                    });
                    
             

                    // Set chart options
                    var options = {
                        'title': 'How Much Pizza I Ate Last Night',
                        'width': 400,
                        'height': 300
                    };

                    // Instantiate and draw our chart, passing in some options.
                    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
                    chart.draw(data, options);

                }

            });


         
        }
    </script>
</head>

<body>
    <!--Div that will hold the pie chart-->
    

    <div class="jumbotron">
        <h1>Gelir Gider Tablosu</h1>
        <button onclick="drawChart();">bilgileri goster</button>
    </div>
    <div id="chart_div"></div>
</body>
</html>

答案 1 :(得分:2)

它正在评估getAnnualSal作为范围内的属性。如果要将其作为范围中的函数执行,请添加()

{{getAnnualSal()}}

答案 2 :(得分:1)

如前所述,您应该使用()作为函数。

我还想提一下,你的函数应该在Angular中使用$scope.sal而不是this.sal

$scope.getAnnualSal = function () {
    return $scope.sal * 12;
}

用法:

Hello {{getAnnualSal()}}