1

我已经定义了一个类,实例化它并将对象归因于范围。ng-model 中的引用已经完成,但我不知道为什么这段代码不起作用。帮帮我,我怀疑 Angular 不适用于使用保留字“类”定义的类

-- app.js
angular.module('myApp', [
    'myApp.controllers'
]);


-- controllers.js
class Finance() {
        constructor() {
            this.salary = 100;
            this.percentage = 10;
        }

        result() {
            return this.salary * this.percentage * 0.01;
        }
    }

var m = angular.module('myApp.controllers', []);
m.controller('FinanceController', function($scope) {

    $scope.f = new Finance();

    console.log($scope.f.salary);
    console.log($scope.f.percentage);
    console.log($scope.result());
});


-- index.html
<!doctype html>
<html lang="en" ng-app="myApp">
<head>
<title>Finance Meter</title>
</head>
<body ng-controller="FinanceController">
    Your Salary?
    <input type="text" ng-model="f.salary">
    <br/>How much should you invest in shopping?
    <input type="text" ng-model="f.percentage"> %
    <br/>The amount to be spent on gadgets will be: <span>{{f.result()}}</span>
    <script src="lib/angular/angular.js"></script>
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
</body>
</html>
4

1 回答 1

0

出现语法错误:

控制台.log($scope.f.result());

于 2017-10-26T13:33:37.803 回答