角度范围不起作用

Angular scope not working

本文关键字:不起作用 范围      更新时间:2023-09-26

我有这样的控制器:

angular.module('app', [])
.controller('ctrl', ['$scope', function ($scope) {
$scope.daysPerMonth = new Date(year, month).getDate();
}]
);

和 html:

<div ng-app>
  <h1>How many days has month?</h1>
  <input ng-model="month" type="text" placeholder="Set a month as number">
  <input ng-model="year" type="text" placeholder="Set a year as number">
  <p ng-if="year" ng-model="daysPerMonth">
    In {{ month }} of {{ year }}, we have {{ daysPerMonth }} days.
  </p>
</div>

为什么它不起作用?

https://jsfiddle.net/m7aLdwe1/

试试这个。你忘了添加ng-contoller和ng-app我只是纠正你的错误。

var app = angular.module('app', []);
	app.controller('ctrl',['$scope', function ($scope) {
      
      //change your logic to compute ... 
      
	}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <h1>How many days has month?</h1>
  
  <input ng-model="month" type="text" placeholder="Set a month as number">
  
  <input ng-model="year" type="text" placeholder="Set a year as number">
  
  <p ng-if="year" >
    In {{ month }} of {{ year }} ......
  </p>
  
</div>