使用状态"onEnter"触发控制器内部的一个函数;函数

Triggering a function inside of a controller using states "onEnter" function

本文关键字:quot 函数 一个 状态 onEnter 控制器 内部      更新时间:2023-09-26

所以我的导航使用的是角状态。每当你点击菜单上的一个项目时,它就会触发一个功能,改变标题中的图片,这样每个页面的标题上就会有不同的图片。当有人从网站的主页开始,只通过点击导航时,这个功能很好,但是当你输入url时,这个功能不会被触发。我试图使用onEnter()来触发它,所以我可以放下点击,它将在两种情况下工作,但它看起来像我有范围问题,可以真正使用一些指导。

app.directive("mainNav", function() {
    return {
        restrict: "E",
        templateUrl: "js/main-nav.html",
        controller: function() {
        this.tab = tabbytab;
        this.isSet = function(checkTab) {
            return this.tab === checkTab;
        };
        this.setTab = function(activeTab) {
            tabbytab = activeTab;
            window.fbAsyncInit();
        };
        this.imageArray = navImages;
        //minus 1 because tabbytab starts at 1 and the array doesnt of course
        this.currentImage = navImages[(tabbytab - 1)].image;
        console.log(tabbytab);
        this.setImage = function(activeTab) {
            this.currentImage = this.imageArray[tabbytab].image;
        };
    },
    controllerAs: "tab"
};
});

这是我的状态

.state('home', {
        url: "/home",
        templateUrl: "js/home.html",
        onEnter: function() {
          tab.setTab(1);
          console.log("on enter function triggered");
        }
    })

您能再解释一下您所说的范围界定问题是什么意思吗?到底发生了什么?

另一种可能不那么复杂的方法是使用$routeParams https://docs.angularjs.org/api/ngRoute/service/。基本上,你可以从路由(url)中拉入参数,并根据url显示图像。