Angularjs:当页面加载时,(第一个选项卡)触发通常由ngClick上的按钮触发的方法 [英] Angularjs: When page loads, (for the first tab) trigger the method that is normally triggered by ngClick on a button

查看:176
本文介绍了Angularjs:当页面加载时,(第一个选项卡)触发通常由ngClick上的按钮触发的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关系到一个标签式界面,可使用不同的社交网络显示时间表/帖子。

一切都很好,我有纵向平铺选项卡的侧边栏。单击选项卡会向资源的调用,并加载标签(社交网络)的帖子。

  NG-点击=loadTabContent('网络')

问题是,网页负载无标签已显示的内容,而我想第一个来已加载(明显)。我想我需要以某种方式触发同样的方法时,页面加载,并将它旨在向第一li元素(顶部标签)。

注:


  1. 由于AngularJS不会加载数据,标签内容包装已经到位 - 它甚至包含了本身的li元素 - 但它们是空的,因为它检索LI元素的实际含量的方法还没有被调用。李元素是有,因为它们是定制指令(仅限于A),并在HTML pre codeD。


解决方案

从我的其他问题一SO用户牢记一个忠告,并在其他网站的一些关键信息磕磕绊绊,我用ngInit指令的第一次,它竟然是正是我需要的。

我的HTML:

  NG-控制器=MyCtrlNG-的init =的init('socialnetwork')

我的控制器:

  $ scope.methodToFireOnLoad =功能(someVariableArgHere){// code}在这里
$ scope.init =功能(argumentFromInit){
    //火所需的方法 - 固定值 - 在我相关的第一个选项卡案值
    $ scope.methodToFireOnLoad(argumentFromInit);
}

This is related to a tabbed interface for displaying timelines/posts from different social networks.

Everything is fine, I have a sidebar with vertically tiled tabs. Click on a tab will make a call to the resource and load that tab (social network)'s posts.

ng-click="loadTabContent('network')"

The issue is that on page load no tab has content displayed, whereas I'd like the first one to be already loaded (obviously). I assume I need to somehow trigger that very same method when page loads and have it aimed towards the first li element (the top tab).

NOTES:

  1. Since AngularJS does loading the data, the tab content wrapper is already in place - it even contains li elements in itself - but they are empty since the method which retrieves the actual content of LI elements hasn't been called. LI elements are there because they are custom directives (restricted to "A"), and are precoded in HTML.

解决方案

Keeping in mind a piece of advice from a SO user from my other question, and stumbling upon some crucial info on another website, I used the ngInit directive for the first time and it turned out to be exactly what I needed.

My HTML:

ng-controller="MyCtrl" ng-init="init('socialnetwork')"

My controller:

$scope.methodToFireOnLoad = function(someVariableArgHere){ // code here }
$scope.init = function(argumentFromInit){
    // fire the desired method - fixed value - in my case value related to the first tab
    $scope.methodToFireOnLoad(argumentFromInit);
}

这篇关于Angularjs:当页面加载时,(第一个选项卡)触发通常由ngClick上的按钮触发的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆