我收到错误:期望定义未定义。当我尝试Visual Studio工作正常,但不在我正在研究的测试网站上。 HTML代码:
<html>
<head>
<script src="lib/angularjs/angular.min.js"></script>
<script src="lib/angularjs/angular-mocks.js"></script>
<script src="index.js" type="text/javascript"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<input type="number" ng-model="number1"><br><br>
<input type="number" ng-model="number2"><br><br>
{{number1}} + {{number2}} = <span style="color:green" type="text">{{number1 + number2}}</span> which is an
<span ng-bind="odd_even(number1 + number2)"></span> number
<br><br>
{{number1}} - {{number2}} = <span style="color:green" type="text">{{number1 - number2}}</span> which is
<span ng-bind="odd_even(number1 + number2)"></span> number
<br><br>
</div>
</body>
</html>
控制器JS代码:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
//add your code here
$scope.odd_even = function(result) {
if(result % 2 == 0) {
return 'even';
}
else if(result % 2 == 1){
return 'odd';
}
}
});
错误:
AngularJS Test Controller: Testing: Check the scope object number1 is defined or not FAILED
Expected undefined to be defined.
at UserContext.<anonymous> (test/index_test.js:19:36)
Node.js (linux; U; rv:v8.9.4) AngularJS Test Controller: Testing: Check the scope object number2 is defined or not FAILED
Expected undefined to be defined.
请帮助!!!
答案 0 :(得分:0)
请您这样尝试,让我知道是否有任何问题?
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
//add your code here
$scope.number1 = 100;
$scope.number2 = 11;
$scope.result = $scope.number1+$scope.number2;
$scope.odd_even = function(result)
{
if(result%2==0)
{
return "even";
}
else if(result % 2 == 1)
{
return "odd";
}
}
});