angularjs – 将对象作为属性传递给已编译的指令

前端之家收集整理的这篇文章主要介绍了angularjs – 将对象作为属性传递给已编译的指令前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在页面上有一个角度元素,需要与其他非角度页面元素进行通信.

我正在创建指令元素,并将其附加到其目标div.我试图传递该创建的指令一个对象(ajax对象),它只包含属性.

问题是我无法弄清楚如何将这个ajax对象传递给指令,因为$compile需要一个范围.当http结束,并且因为我必须在指令中使用=时,指令被覆盖.

请看我的插件https://plnkr.co/edit/brTWgUWTotI44tECZXlQ(抱歉图像).点击< button>触发指令.

  1. (function() {
  2. 'use strict';
  3. var CHANNEL = 'podOverlay';
  4. angular.module('CavernUI',[])
  5. .controller('CavernCtrl',function($scope,getItemService) {
  6.  
  7. $scope.model = {};
  8. var _pods = $scope.model.pods = {};
  9.  
  10. function getData(selector) {
  11. $(selector).each(function(i,pod) {
  12. _pods[+pod.dataset.item] = {
  13. $: $(pod)
  14. };
  15. });
  16.  
  17. Object.keys($scope.model.pods).map(function(key) {
  18. getItemService.getItem(key).success(function(response) {
  19. _pods[key] = angular.extend(_pods[key],response);
  20. $scope.$broadcast(CHANNEL,_pods[key],$scope);
  21. });
  22. })
  23. }
  24.  
  25. $scope.runPodCheck = function(selector) {
  26. getData(selector);
  27. }
  28. })
  29. .directive('podchecker',function($compile) {
  30.  
  31. var createOverlay = function(e,data,scope){
  32. scope.data = data;
  33. // can i just pass data rather than scope.data?
  34. // If I pass the scope,then when another $broadcast happens
  35. // the scope updates,wiping out the last scope change.
  36. // Scope here really needs to be a static object that's
  37. // created purely for the hand off. But I don't know if
  38. // that can be done.
  39. angular.element(data.$[0]).empty().append($compile('<overlay data="data"></overlay>')(scope));
  40. }
  41.  
  42. return {
  43. restrict: 'E',scope: {
  44. check: '&',},templateUrl: 'tpl.html',link: function(scope,elm,attr){
  45. scope.$on(CHANNEL,createOverlay);
  46. }
  47. };
  48. })
  49. .directive('overlay',function() {
  50. return {
  51. restrict: 'E',scope: {
  52. o: '=data' // here is the problem.
  53. },template: '<div class="overlay"><a href="{{o.url}}"><img ng-src="{{o.images.IT[0]}}"/></a></div>',attr) {
  54.  
  55. }
  56. }
  57. })
  58. .service('getItemService',['$http',function($http) {
  59. this.getItem = function(itemId) {
  60. return $http({
  61. method: 'GET',url: 'https://www.aussiebum.com/ajaxproc/item',params: {
  62. id: itemId,ajxop: 1
  63. },});
  64. };
  65. }]);
  66. }());

编辑:
预期产量:

我不确定这是最好的方法,但一种方法可能是为每个叠加层手动创建一个新的范围.

所以改变了这个:

  1. var createOverlay = function(e,scope){
  2. scope.data = data;
  3. angular.element(data.$[0]).empty().append($compile('<overlay data="data"></overlay>')(scope));
  4. }

对此:

  1. var createOverlay = function(e,scope){
  2. var overlayScope = scope.$new(false); // use true here for isolate scope,false to inherit from parent
  3. overlayScope.data = data;
  4. angular.element(data.$[0]).empty().append($compile('<overlay data="data"></overlay>')(overlayScope));
  5. }

更新了Plnkr:https://plnkr.co/edit/wBQ1cqVKfSqwqf04SnPP

有关更多信息$new()

干杯!

猜你在找的Angularjs相关文章