3
我開始學習AngularJs所以我通過創建一個視圖,分別服務文件和控制文件開始了這種方式,如下所示的函數:AngularJs:參數是不是得了未定義
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="ServicesTut.js"></script>
<script src="ControllerTut.js"></script>
</head>
<body>
<div class="container">
<div ng-controller="CalculatorController">
Enter a number:
<input type="number" ng-model="number">
<button ng-click="findSquare()">Square</button>
<div>{{answer}}</div>
</div>
</div>
</body>
</html>
這是我的服務文件 - - ServicesTut.js」
var CalculatorService = angular.module('CalculatorService', [])
app.service('Calculator', function() {
this.square = function (a) { return a*a};
});
這是我的控制器文件--- ControllerTut.js
var myApp = angular.module('myApp', ['CalculatorService']);
app.controller('CalculatorController', function ($scope, Calculator) {
$scope.findSquare = function() {
$scope.answer = Calculator.square($scope.number);
}
});
關於開展我的瀏覽器中,我得到了以下錯誤消息的代碼:
Error: Argument 'CalculatorController' is not a function, got undefined
請幫助我!
你太棒了。問題回答了!此外,除了我更新我的控制器文件從app.controller .....到myApp.controller。謝謝 – Blaze
@JerryGreen很高興幫助你。謝謝:) –