简体   繁体   English

AngularJS用函数更改类

[英]AngularJS changing class with function

I have an Agular app where I have a list of US states layout out as a UL in a "block" fashion. 我有一个Agular应用程序,其中有一个美国州列表以“块”方式布置为UL。

http://jsfiddle.net/Mrbaseball34/3u375/ http://jsfiddle.net/Mrbaseball34/3u375/

Now, when the states variable has an entry, the class on the particular state should be active (showing in a maroon color). 现在,当状态变量具有条目时,特定状态的类应处于活动状态(以栗色显示)。 I'm setting the class using a function because you cannot us the in clause. 我正在使用函数设置类,因为您不能使用in子句。 The code DOES go into the function and it DOES return true when the state == 'TX', however, the class is not being applied. 代码确实进入该函数,并且当状态=='TX'时,它确实返回true,但是,未应用该类。

Template: 模板:

<div ng-controller="MyCtrl" id="state_scroller">
    <section class="states">
        <ul>
            <li ng-repeat="state in statesList" 
                ng-class="{true: 'active', false: ''}[selectedState($index)]"  
                id="st_{{state.id}}">{{state.id}}</li>
        </ul>
    </section>
</div>

What am I doing wrong here? 我在这里做错了什么? Here's what the rendered code looks like in the Chrome debugger: 这是在Chrome调试器中呈现的代码:

<div ng-controller="MyCtrl" id="state_scroller" class="ng-scope">
    <section class="states">
        <ul>
            <!-- ngRepeat: state in statesList -->
            <li ng-repeat="state in statesList" 
                ng-class="{true: 'active', false: ''}[selectedState($index)]" 
                id="st_AK" class="ng-scope ng-binding">AK</li>
            <!-- end ngRepeat: state in statesList -->
   <!-- snipped some -->
            <li ng-repeat="state in statesList" 
                ng-class="{true: 'active', false: ''}[selectedState($index)]" 
                id="st_TN" class="ng-scope ng-binding">TN</li>
            <!-- end ngRepeat: state in statesList -->
            <li ng-repeat="state in statesList" 
                ng-class="{true: 'active', false: ''}[selectedState($index)]" 
                id="st_TX" class="ng-scope ng-binding">TX</li>
            <!-- end ngRepeat: state in statesList -->
            <li ng-repeat="state in statesList" 
                ng-class="{true: 'active', false: ''}[selectedState($index)]" 
                id="st_UT" class="ng-scope ng-binding">UT</li>
            <!-- end ngRepeat: state in statesList -->
   <!-- snipped rest -->
            <!-- end ngRepeat: state in statesList -->
        </ul>
    </section>
</div>
$scope.selectedState = function(id) {
    var x = false;
    angular.forEach($scope.states, function (state, key2) {
        if (state.id == $scope.statesList[id].id) {
            x = true;
        }
    })
    return x;
};

You have to check everything, THEN return if you found it. 您必须检查所有内容,然后找到就返回。 Right now if the first one doesn't match, you exit out of the foreach. 现在,如果第一个不匹配,则退出foreach。 And then you don't return from the selectedState function (the foreach has it's own function, which returns back to the selectedState function, which then doesn't return anything. 然后,您不必从selectedState函数返回(foreach具有它自己的函数,该函数将返回到selectedState函数,该函数随后将不返回任何内容。

http://jsfiddle.net/3u375/17/ http://jsfiddle.net/3u375/17/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM