如何在Angular JS中仅为父元素使用$ event?

时间:2015-09-05 09:06:41

标签: angularjs

我有ng-click事件的HTML代码:

<div class="btn share" ng-click="do($event)">
   <span">1</span>
</div>

Angular JS:

$scope.do = function (event) {
  var target = angular.element(event.target);
  var parsed = parseInt(target.find('span').text(), 10);
}

当我点击元素div或子元素span被称为事件do()时。 但如果我点击span span内的我的计数器不是增量。仅通过单击父元素div。 如何为$eventdiv元素设置相同的span

3 个答案:

答案 0 :(得分:4)

我建议使用范围绑定而不是DOM textContent:

// DirectShow interfaces
IGraphBuilder *pGB = NULL;
IMediaControl *pMC = NULL;
IMediaEventEx *pME = NULL;
IBasicAudio *pBA = NULL;
IMediaSeeking *pMS = NULL;
IMediaPosition *pMP = NULL;
// VMR9 interfaces
IVMRWindowlessControl9 *pWC = NULL;

#define JIF(x) if (FAILED(hr=(x))) \
{Msg(TEXT("FAILED(hr=0x%x) in ") TEXT(#x) TEXT("\n\0"), hr); return hr; }

HRESULT PlayMovieInWindow(LPTSTR szFile)
{
HRESULT hr;
// hr = CoInitialize(NULL);
// Get the interface for DirectShow's GraphBuilder
JIF(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
    IID_IGraphBuilder, (void **)&pGB));

if (SUCCEEDED(hr))
{
    IBaseFilter *pVmr;

    // Create the VMR and add it to the filter graph.
    HRESULT hr = CoCreateInstance(CLSID_VideoMixingRenderer9, NULL,
        CLSCTX_INPROC, IID_IBaseFilter, (void**)&pVmr);
    if (SUCCEEDED(hr))
    {
        hr = pGB->AddFilter(pVmr, L"Video Mixing Renderer 9");

        //Add LAV Filters to filter graph
        IBaseFilter *pLavSplitterSource, *pLavVideoDecoder, *pLavAudioDecoder, *pAudioRender;
        IFileSourceFilter *pFileSourceFilter;

        hr = AddFilterByCLSID(pGB, CLSID_LavSplitter_Source, &pLavSplitterSource, L"Lav Splitter Source");
        hr = pLavSplitterSource->QueryInterface(IID_IFileSourceFilter, (void **)&pFileSourceFilter);
        hr = pFileSourceFilter->Load(szFile, NULL);

        hr = AddFilterByCLSID(pGB, CLSID_LavVideoDecoder, &pLavVideoDecoder, L"Lav Video Decoder");
        hr = AddFilterByCLSID(pGB, CLSID_LavAudioDecoder, &pLavAudioDecoder, L"Lav Audio Decoder");
        hr = AddFilterByCLSID(pGB, CLSID_DSoundRender, &pAudioRender, L"DirectSound Audio render");

        if (SUCCEEDED(hr))
        {
            // Set the rendering mode and number of streams
            IVMRFilterConfig9 *pConfig;

            JIF(pVmr->QueryInterface(IID_IVMRFilterConfig9, (void**)&pConfig));
            JIF(pConfig->SetRenderingMode(VMR9Mode_Windowless));

            hr = pVmr->QueryInterface(IID_IVMRWindowlessControl9, (void**)&pWC);
            if (SUCCEEDED(hr))
            {
                JIF(pWC->SetVideoClippingWindow(hVideo));
                JIF(pWC->SetBorderColor(RGB(0, 0, 0)));
            }
            SAFE_RELEASE(pConfig);
            SAFE_RELEASE(pVmr);
        }
    }

    // Render the file programmatically to use the VMR9 as renderer.
    // Pass TRUE to create an audio renderer also.
    if (FAILED(hr = RenderFileToVideoRenderer(pGB, szFile, FALSE))) return hr;

    // QueryInterface for DirectShow interfaces
    JIF(pGB->QueryInterface(IID_IMediaControl, (void **)&pMC));
    JIF(pGB->QueryInterface(IID_IMediaEventEx, (void **)&pME));
    JIF(pGB->QueryInterface(IID_IMediaSeeking, (void **)&pMS));
    JIF(pGB->QueryInterface(IID_IMediaPosition, (void **)&pMP));
    JIF(pGB->QueryInterface(IID_IBasicAudio, (void **)&pBA));

    // Have the graph signal event via window callbacks for performance
    JIF(pME->SetNotifyWindow((OAHWND)hVideo, WM_GRAPHNOTIFY, 0));

    JIF(pMC->Run());
}
return hr;
}

void CloseInterfaces(void)
{
HRESULT hr;
// Stop media playback
if (pMC)
    hr = pMC->Stop();

// Disable event callbacks
if (pME)
    hr = pME->SetNotifyWindow((OAHWND)NULL, 0, 0);

// Enumerate the filters And remove them
IEnumFilters *pEnum = NULL;
hr = pGB->EnumFilters(&pEnum);
if (SUCCEEDED(hr))
{
    IBaseFilter *pFilter = NULL;
    while (S_OK == pEnum->Next(1, &pFilter, NULL))
    {
        // Remove the filter.
        pGB->RemoveFilter(pFilter);
        // Reset the enumerator.
        pEnum->Reset();
        pFilter->Release();
    }
    pEnum->Release();
}

// Release and zero DirectShow interfaces
SAFE_RELEASE(pME);
SAFE_RELEASE(pMS);
SAFE_RELEASE(pMP);
SAFE_RELEASE(pMC);
SAFE_RELEASE(pBA);
SAFE_RELEASE(pWC);
SAFE_RELEASE(pGB);
// CoUninitialize();
}

并在控制器中

<div class="btn share" ng-click="do()">
   <span>{{count}}</span>
</div>

如果你仍然想知道为什么它的方法失败了,那是因为你需要使用$scope.count = 0; $scope.do = function () { $scope.count++; }; ,而不仅仅是currentTarget

&#13;
&#13;
target
&#13;
angular.module('demo', []).controller('MainCtrl', function($scope) {
    $scope.do = function(event) {
        var target = angular.element(event.currentTarget);
        var parsed = parseInt(target.find('span').text(), 10);
        target.find('span').text(parsed + 1);
    };

});
&#13;
&#13;
&#13;

  

但是不要这样做,控制器根本不应该使用DOM,这是   不是控制器的用途。

答案 1 :(得分:1)

你做错了。 Angular不是jQuery。您不应该在控制器中执行任何 DOM操作。视图应该基于模型生成,反之亦然。

代码应为

<div class="btn share" ng-click="do()">
    <span>{{ counter }}</span>
</div>

并在控制器中:

$scope.counter = 1;
$scope.do = function() {
    doSomethingWithCounter($scope.counter);
}

答案 2 :(得分:1)

您是否尝试过将相同的点击事件添加到您的范围内?

喜欢这个

<span ng-click="do($event)"> 1 </span>