Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度ng重复不';在茉莉花试验中不起作用_Javascript_Angularjs_Angularjs Directive_Karma Jasmine - Fatal编程技术网

Javascript 角度ng重复不';在茉莉花试验中不起作用

Javascript 角度ng重复不';在茉莉花试验中不起作用,javascript,angularjs,angularjs-directive,karma-jasmine,Javascript,Angularjs,Angularjs Directive,Karma Jasmine,我试图让ng repeat在Angular指令测试中工作,但当更新$scope变量并调用$digest()时,视图不会更新 'use strict'; describe('Asset tests', function() { var $compile, $rootScope, $scope, $timeout, templateHtml = '<span ng-repeat="asset in assets">

我试图让ng repeat在Angular指令测试中工作,但当更新$scope变量并调用$digest()时,视图不会更新

'use strict';

describe('Asset tests', function() {
    var $compile,
        $rootScope,
        $scope,
        $timeout,
        templateHtml = '<span ng-repeat="asset in assets">{{asset.id}}</span>',
    view;

    beforeEach(function() {
        module('my.module');
        inject(function(_$compile_, _$rootScope_, _$timeout_) {
            $compile = _$compile_;
            $rootScope = _$rootScope_;
            $timeout = _$timeout_;
            $scope = $rootScope.$new();

            $scope.assets = [];
            $scope.selectedItems = [];
            $scope.deleteAsset = function() {
                console.log('yay');
            };
            view = angular.element(templateHtml);
            $compile(view)($scope);
            $scope.$apply();
        });
    });

    describe('Testing some stuff', function() {
        it('should test some stuff', function() {
            for(var i = 0; i < 3; i ++) {
                $scope.assets.push({id:i, name: 'Rad ' + i});
            };
            $scope.$digest();
            console.log(view);
            console.log(jQuery('span').length);
        });
    });
});
“严格使用”;
描述('资产测试',功能()){
var$compile,
$rootScope,
$scope,
$timeout,
templateHtml='{{asset.id}}',
看法
beforeach(函数(){
模块(“我的模块”);
注入(函数($compile,$rootScope,$timeout){
$compile=\$compile;
$rootScope=\u$rootScope;
$timeout=$timeout;
$scope=$rootScope.$new();
$scope.assets=[];
$scope.selectedItems=[];
$scope.deleteAsset=函数(){
console.log('yay');
};
视图=angular.element(templateHtml);
$compile(view)($scope);
$scope.$apply();
});
});
描述('测试一些东西',函数(){
它('应该测试一些东西',函数(){
对于(变量i=0;i<3;i++){
$scope.assets.push({id:i,name:'Rad'+i});
};
$scope.$digest();
console.log(视图);
log(jQuery('span').length);
});
});
});
日志语句输出:

LOG: {0: <span ng-repeat="assets in assets">{{asset.id}}</span>, length: 1}   
LOG: 56
LOG:{0:{{asset.id},长度:1}
日志:56

我认为您的html模板编译不正确。它表示
资产中的资产
,应该是
资产中的资产
。试试看

另外,我认为你没有编译正确的东西。您正在编译文本模板,但不是实际的视图元素

view = angular.element(templateHtml);
$compile(view)($scope);

这个问题很老了,但我遇到了一个类似的问题,所以我发布了对我有用的东西。 我的问题在模板中。我需要添加一个div或其他东西来包装有ng重复的跨度。在这种情况下,更改为:

templateHtml = '<span ng-repeat="asset in assets">{{asset.id}}</span>'
templateHtml='{{asset.id}}'

templateHtml='{{asset.id}}'
我会成功的


希望它能帮助别人。

谢谢你指出这些事情。仍然得到同样的结果。查看原始代码的编辑。当然有帮助(通过一个玩笑测试)。谢谢
templateHtml = '<div><span ng-repeat="asset in assets">{{asset.id}}</span></div>'