为什么这样的功能和特性,present控制器内未被访问? [英] Why this function and property, present inside of controller is NOT being accessed?

查看:119
本文介绍了为什么这样的功能和特性,present控制器内未被访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

app2.js

app2.js

(function(){
    var app = angular.module("panel",[]);
    app.controller('PanelController',function(){
          this.tab = 1;

          this.setTab = function(setTab){
                this.tab = setTab;
          };


    });

})();

以及 视图 是:

And the view is:

<html ng-app="panel">

<head>
    <link rel="stylesheet" type="text/css" href="bootstrap.min.css">
    <script type="text/javascript" src="angular.js"></script>
    <script type="text/javascript" src="app2.js"></script>\
    <style type="text/css">
    li{
        width:100px;
    }

    </style>
</head>

<body>

<section ng-controller="PanelController as panel">
    <ul class="nav nav-pills" >
        <li ng-class="{active:tab === 1}"><a ng-click="panel.setTab(1)" href="#">1</a></li>
        <li ng-class="{active:tab === 2}"><a ng-click="tab = 2" href="#">2</a></li>
        <li ng-class="{active:tab === 3}"><a ng-click="tab = 3" href="#">3</a></li>
        <li ng-class="{active:tab === 4}"><a ng-click="tab = 4" href="#">4</a></li>
    </ul>
    <h1>{{tab}}</h1>

    <div class="panel">
        <p ng-show="tab===1">Suppose this is Data coming from object for 1 </p>
        <p ng-show="tab===2">Suppose this is Data coming from object for 2 </p>
        <p ng-show="tab===3">Suppose this is Data coming from object for 3 </p>
        <p ng-show="tab===4">Suppose this is Data coming from object for 4 </p>

    </div>



</section>



</body>
</html>

问题是与 panel.set(1),当&LT不被调用;一个GT&; 点击。为什么会这样呢?无论是财产设置页控制器内被访问。

Problem is with panel.set(1) which is not being invoked when <a> is clicked. Why is it so? Neither the property tab inside of controller is being accessed.

推荐答案

panel.setTab 被调用。该问题是由设置页变量在模板所致。你应该 panel.tab 替换它们。

panel.setTab is invoked. The problem is caused by the tab variable in the template. You should replace them with panel.tab.

&lt;节NG控制器=PanelController作为面板&GT; 表示

$scope.panel = new PanelController();

所以如果你想用你的控制器内部变量。您应该添加面板。 preFIX,否则就意味着 $ scope.tab

so if you want to use variable inside your controller. You should add panel. prefix, otherwise it means $scope.tab

这篇关于为什么这样的功能和特性,present控制器内未被访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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