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

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

问题描述

这与用于显示来自不同社交网络的时间线/帖子的选项卡式界面有关.

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')"

问题是页面加载时没有选项卡显示内容,而我希望第一个已经加载(显然).我假设我需要在页面加载时以某种方式触发相同的方法,并将其指向第一个 li 元素(顶部选项卡).

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).

注意:

  1. 由于 AngularJS 确实加载了数据,因此选项卡内容包装器已经就位 - 它甚至本身包含 li 元素 - 但它们是空的,因为尚未调用检索 LI 元素实际内容的方法.存在 LI 元素是因为它们是自定义指令(仅限于A"),并且在 HTML 中进行了预编码.

推荐答案

牢记 SO 用户从我的另一个问题中得到的一条建议,并在另一个网站上偶然发现了一些关键信息,我使用了 ngInit 指令第一次,结果正是我所需要的.

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.

我的 HTML:

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

我的控制器:

$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天全站免登陆