1
클래스를 정의하고 인스턴스화 한 다음 객체의 범위를 속성으로 지정했습니다. ng-model의 참조가 완료되었지만이 코드가 작동하지 않는 이유를 알 수 없습니다. 나는 각도가 예약어 '클래스'로 정의 클래스와 함께 작동하지 않습니다 의심 도와주세요ng-model의 객체 속성
구문 오류가 발생했습니다-- 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>
다음과 같이 제어기를 정의 할 수 있습니다 :'m.controller ('Finance', Finance)'(제어기에 대한 모든 주입은 생성자 내부에 들어갑니다) –
오타가있을 수 있지만 $ scope.f가되어서는 안됩니다. 결과()? – Mickers