describe('some function',function () {
it('should do something',inject(function ($compile,$rootScope) {
var element = $compile('<div id = "foo" ng-hide = "somecondition">Bar</div>')($Scope);
$rootScope.digest();
//Search for a given DOM element and perform some test here
}));
});
describe('some function',function () {
//Get the div with ID 'foo' in the compiled template
var elm = $('#foo');
expect(elm.css('display')).toEqual('none');
});
});
(function() {
'use strict';
var CHANNEL = 'podoverlay';
angular.module('CavernUI',[])
.controller('CavernCtrl',function($scope,getItemService) {
$scope.model = {};
var _pods = $scope.model.pods = {};
function getData(selector) {
$(selector).each(function(i,pod) {
_pods[+pod.dataset.item] = {
$: $(pod)
};
});
Object.keys($scope.model.pods).map(function(key) {
getItemService.getItem(key).success(function(response) {
_pods[key] = angular.extend(_pods[key],response);
$scope.$broadcast(CHANNEL,_pods[key],$scope);
});
})
}
$scope.runPodCheck = function(selector) {
getData(selector);
}
})
.directive('podchecker',function($compile) {
var createOverlay = function(e,data,scope){
scope.data = data;
// can i just pass data rather than scope.data?
// If I pass the scope,then when another $broadcast happens
// the scope updates,wiping out the last scope change.
// Scope here really needs to be a static object that's
// created purely for the hand off. But I don't kNow if
// that can be done.
angular.element(data.$[0]).empty().append($compile('<overlay data="data"></overlay>')(scope));
}
return {
restrict: 'E',scope: {
check: '&',},templateUrl: 'tpl.html',link: function(scope,elm,attr){
scope.$on(CHANNEL,createOverlay);
}
};
})
.directive('overlay',function() {
return {
restrict: 'E',scope: {
o: '=data' // here is the problem.
},template: '<div><a href="{{o.url}}"><img ng-src="{{o.images.IT[0]}}"/></a></div>',attr) {
}
}
})
.service('getItemService',['$http',function($http) {
this.getItem = function(itemId) {
return $http({
method: 'GET',url: 'https://www.aussiebum.com/ajaxproc/item',params: {
id: itemId,ajxop: 1
},});
};
}]);
}());
编辑: 预期产量:
我不确定这是最好的方法,但一种方法可能是为每个叠加层手动创建一个新的范围.
所以改变了这个:
var createOverlay = function(e,scope){
scope.data = data;
angular.element(data.$[0]).empty().append($compile('<overlay data="data"></overlay>')(scope));
}
对此:
var createOverlay = function(e,scope){
var overlayScope = scope.$new(false); // use true here for isolate scope,false to inherit from parent
overlayScope.data = data;
angular.element(data.$[0]).empty().append($compile('<overlay data="data"></overlay>')(overlayScope));
}