小编has*_*sin的帖子

Angularjs pubsub vs $ broadcast

我一直在阅读Angularjs的事件传递,我不相信使用$ broadcast是一个好主意.

像这样的博客一个倡导习惯$即使它"感觉就像矫枉过正."

我的困惑是,实现使用范围的深度优先遍历并查找订阅者,这使得事件的速度取决于您的树结构.以下是角度中的一些代码:

// Insanity Warning: scope depth-first traversal
// yes, this code is a bit crazy, but it works and we have tests to prove it!
// this piece should be kept in sync with the traversal in $digest
if (!(next = (current.$$childHead || (current !== target && current.$$nextSibling)))) {
   while(current !== target && !(next = current.$$nextSibling)) {
     current = current.$parent;
   }
}
Run Code Online (Sandbox Code Playgroud)

此外,您似乎可以使用这些方法破解依赖注入.

替代方案只是一种缓存事件类型和回调的服务,并直接调用它们.这要求您清理订阅以避免泄漏.

我的问题是,对于$ broadcast/$范式的动机,我有什么遗漏吗?或者在更传统的pubsub上使用它有什么好处?

如果我对自己的问题不够清楚,请告诉我,谢谢你的时间.

javascript publish-subscribe angularjs

32
推荐指数
1
解决办法
8484
查看次数

无法找到指令所需的控制器

我有一个指令,我想要另一个指令,以便能够调用.我一直在尝试使用指令控制器来尝试实现这一点.

指令1将与指令2位于同一页面上,指令1将调用指令2的控制器公开的方法:

指令1:

'use strict';
angular.module('angularTestApp')
    .directive('fileLibrary', function () {
        return {
            templateUrl: 'views/manage/file_library/file-library.html',
            require: 'videoClipDetails',
            restrict: 'AE',
            link: function postLink(scope, element, attrs, videClipDetailsCtrl) {
                scope.doSomethingInVideoClipDirective = function() {
                    videClipDetailsCtrl.doSomething();
                }
            }
        };
    });
Run Code Online (Sandbox Code Playgroud)

指令二:

'use strict';
angular.module('angularTestApp')
    .directive('videoClipDetails', function () {
        return {
            templateUrl: 'views/video_clip/video-clip-details.html',
            restrict: 'AE',
            controller: function($scope, $element) {
                this.doSomething = function() {
                    console.log('I did something');
                }
            },
            link: function postLink(scope, element, attrs) {
                console.log('videoClipDetails directive');
                //start the element out as hidden
            }
        };
    });
Run Code Online (Sandbox Code Playgroud)

使用两者并将其设置为兄弟姐妹的文件:

<div> …
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-directive angularjs-scope

23
推荐指数
2
解决办法
3万
查看次数

Google Maps API v3与自定义图标的兴趣点

我有一个页面拉着我给定区域的学校,教堂和公园,但我想用3个不同的图标设计3个POI.我搜索了Google甚至所有文档,但找不到答案.

var map;
var infowindow;

function initialize() {

  // Center of Map
  var centerLatlng = new google.maps.LatLng(29.745376, -95.380125);

  // Marker Icons Declaration
  var icon = new google.maps.MarkerImage("smLinks-twitter.png", null, null, new google.maps.Point(41,47));

  // Map Options
  var myOptions = {
    zoom: 16,
    center: centerLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    icons: icon 
  };

  // Draw the map
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  // Marker Icons Implementation
  markers = new google.maps.Marker({ 
    position: centerLatlng,
    map: map, 
    title: 'Center of Map', 
    icon: icon 
  });

  // Services: Places
  var request …
Run Code Online (Sandbox Code Playgroud)

google-maps

8
推荐指数
1
解决办法
1万
查看次数

了解bindonce限制

我一直在阅读关于bindonce作为减少手表和提高性能的方法.为了更好地理解包装,我做了一个例子ng-repeat.

JSBIN在这里

没有bindonce我得到103个手表,100个列表项+ 2个按钮.

使用bindonce我获得3个手表,2个底部+ 1个列表.

如果我理解binonce正确,它会在解析并渲染绑定对象后删除手表.所以,

如何使用bindonce对对象所做的更改仍然可以反映在DOM中?

angularjs angularjs-directive bindonce

7
推荐指数
1
解决办法
1996
查看次数

以编程方式触发Firefox中的滚动

我有一个相当人为的例子,你可以滚动一个蓝色div,一个事件监听器将接受事件并将其克隆到另一个div.我想要滚动另一个div.

这个jsfiddle在chrome(版本42.0.2311.135(64位))中运行得很好.

fake.addEventListener('mousescroll', function(e) {
  var clonedEvent = new e.constructor(e.type, e);
  container.dispatchEvent(clonedEvent);
});
Run Code Online (Sandbox Code Playgroud)

但是,仅使用Firefox事件DOMMouseScroll不起作用.它抛出一个错误TypeError: Illegal constructor.

如果我手动创建一个新事件,它也不起作用.Firefox版本37.0.2.

var clonedEvent = document.createEvent("MouseEvents");
clonedEvent.initMouseEvent( ... /* data from e */);
Run Code Online (Sandbox Code Playgroud)

我已经能够捕获事件并scrollTop根据事件中的增量进行设置,但是如果您手动滚动可滚动的div,则不会使用相同的动画滚动.

如何通过JavaScript触发div的本机滚动行为?我不想使用jQuery滚动.

javascript firefox

5
推荐指数
0
解决办法
245
查看次数