ng-if表达式未正确评估

时间:2016-06-24 21:28:16

标签: javascript jquery html angularjs

我非常困惑为什么我的ng-if表达式没有像我认为的那样进行评估。我正在从上一页获取该按钮的ID,并尝试使用ng-if表达式将该ID与下一页上的新ID匹配。

我的index.html页面根据我的JSON对象数据产生按钮数量,然后当点击一个按钮时,它应该进入我的getButtonClicked范围,并点击按钮的正确ID。在范围中,我将此正确的ID存储在sessionStorage中。

的index.html

    <div ng-controller="AppCtrl">
        <button ng-repeat="x in data" id="{{x.id}}" ng-click="getButtonClicked(x.id)"  onclick="window.location='secondPage.html';" >{{x.id}} {{x.userFirstName}} {{x.userLastName}}</button>
    </div>

retrieveData.js

// creates a module
var myApp = angular.module("myModule",[]);


myApp.controller('AppCtrl' , function($scope, $http){

        $http.get("thisurlworksjustreplacingip")
        .success(function(response) {$scope.data = response;});

        $scope.getButtonClicked = function(buttonNumber){
            sessionStorage.setItem("school", buttonNumber);
        }
});

function returnSchool(){
    return (sessionStorage.getItem("school"));
}

然后在我的secondPage.html上,我尝试将sessionStorage变量(我在returnSchool()中返回)与ng-if中的按钮ID相匹配。因此,如果您单击第一页上的第二个按钮,它应该在第二页上显示具有相同数据的按钮。最终我会更改按钮上显示的数据,我只想在我这样做之前正确匹配ID。

secondPage.html

<div ng-controller="AppCtrl">
    <button ng-repeat="x in data" id="{{x.id}}" ng-if="x.id == returnSchool()">X ID: {{x.id}} Get Value: {{returnSchool()}} {{x.userFirstName}} {{x.userLastName}}</button>
</div>
<script>console.log(returnSchool());</script>

我不明白的是,当我尝试使用angular打印出按钮时,returnSchool()将不会打印任何内容。当我在下面的控制台中打印出returnSchool()时,它会打印出我在最后一页(1,2,3,4)上单击的按钮的正确ID。如果我更改了ng-if =&#34; x.id == returnSchool()&#34;,它没有显示任何按钮,则更改为ng-if =&#34; x.id!= returnSchool()&#34; ,它打印所有按钮,让我相信也许returnSchool()不等于上一页按下的按钮ID?我认为有些东西在我的ng-if中没有正确评估。

0 个答案:

没有答案