2017-02-10 79 views
0

我有几篇关于这方面的文章,但没有得到适合我的问题的解决方案。我采用了棱角分明的js 1.5和Bootstrap 3如何通过Angular js获取元素的ID

我的代码是什么样子,

<a href="" class="noUnderline" data-trigger="focus" data-toggle="popover" data-placement="bottom" data-content = "{{helpSrv.helpMessage}}" > 
<i class="fa fa-info-circle infoCirc" ng-click="helpSrv.showPopOverInfo($event)" id="forecastAttainmentId"> </i> 
</a> 

我想说明的酥料饼时,将点击i图标。现在,helpSrv是用Angular JS编写的一项服务。 helpMessage由元素的id选择。 helpSrv是包含像两件事服务,

var s = this 
s.helpInfo = { 
"forecastAttainmentId": "Some text" 
} 
s.showPopOverInfo = function (event) {  
     var helpSelect = event.target.id 
     s.helpMessage = s.helpInfo[helpSelect] 
     console.log(s.helpMessage) 
} 

现在,当我点击了第一次i图标它显示在酥料饼{{helpSrv.helpMessage}}。下次显示实际信息,即Some text。我认为延迟是由于函数调用造成的。如何解决?或者有什么办法可以使用angular(不带任何插件)自动获取元素的id,这样我就可以使用data-content="{{helpSrv.helpInfo['id of the element']}}"来获取Popover的消息。

+0

它真的是'ng-lick'吗?你拼错了,它应该是'ng-click' – pryxen

回答

0

您可以使用jQuery通过使用$("#forecastAttainmentId").click(function(){...}来访问i标记。您还需要初始化helpSrv.helpMessage,以便在第一次加载弹出窗口时能够看到数据。

0

这是关于AngularJs digest cycle。尝试使用$超时:

s.showPopOverInfo = function (event) {  
    $timeout(function(){ 
     var helpSelect = event.target.id; // Always end lines with a semicolon 
     s.helpMessage = s.helpInfo[helpSelect]; 
     console.log(s.helpMessage); 
    }, 0); 
}; 
+0

我已经使用了$超时。但仍然没有帮助 – Anijit

+0

请在jsfiddle或jsbin等上创建一个例子 –