我是用Jasmine测试Angular Apps的新手,我无法弄清楚这个问题的原因......
控制器
programsModule.controller('ticketCtrl', ['$scope', function ($scope) {
$scope.disableSendBtn = true;
});
Run Code Online (Sandbox Code Playgroud)
这是单元测试
'use strict';
describe('app', function () {
// variables
var $rootScope, $scope, $controller;
beforeEach(module('supportModule'));
describe('ticketCtrl', function () {
beforeEach(inject(function (_$rootScope_, _$controller_) {
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
$controller = _$controller_('ticketCtrl', {
'$scope': $scope
});
}));
it('Should disable send btn', function () {
expect($scope.disableSendBtn).toEqual(true);
});
});
});
Run Code Online (Sandbox Code Playgroud)
这是测试的结果
TypeError: Cannot read property 'disableSendBtn' of undefined
Run Code Online (Sandbox Code Playgroud)
如果我测试$scope变量是否定义了
it('Should $scope be defined', function () {
expect($scope).toBeDefined();
});
Run Code Online (Sandbox Code Playgroud)
我也得到这个错误 …
当我添加像h1with这样的元素时margin: 30px 0;,边距会超出容器!
我以前多次遇到这个问题,我通过使用解决了它 overflow: hidden
我想弄清楚是什么问题以及为什么这个解决方案有效?
在这里找到一个 JSFiddle https://jsfiddle.net/LeoAref/zv6c2c2d/
.container {
background: #ccc;
}
.container.overflow {
overflow: hidden;
}
.secTitle {
margin: 30px 0;
}
code {
color: blue;
}Run Code Online (Sandbox Code Playgroud)
<!-- secTitle margin goes outside the container -->
<div class="container">
<h1 class="secTitle">Container without <code>overflow: hidden</code></h1>
</div>
<!-- works fine here -->
<div class="container overflow">
<h1 class="secTitle">Container with <code>overflow: hidden</code></h1>
</div>Run Code Online (Sandbox Code Playgroud)
我在这里做了一个JSFiddle ,我想知道为什么更改函数触发的任何项目的Done复选框?getDoneNum
HTML:
<h3>Done items number: {{getDoneNum()}}</h3>
..
..
<tr ng-repeat="item in items">
<td>{{item.name}}</td>
<td><input type="checkbox" ng-model="item.done"></td>
</tr>
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
$scope.getDoneNum = function () {
return $scope.items.filter(function(item){
return item.done;
}).length;
};
$scope.items = [
{
name: 'Tony',
done: false
},
{
name: 'Emo',
done: true
}
];
Run Code Online (Sandbox Code Playgroud)
我没用ng-change="getDoneNum()",为什么要解雇?