简体   繁体   中英

ng-checked is true but not enabled?

My data looks like, where each entry is log , coming from SupportService

var getDummyLogsForNow = function () {
    return [
        {
            "fileName": "file A",
            "location": "logs/fileA",
            "size": "20000",
            "lastUpdated": "May 04, 2015 09:21PM"
        },
        {
            "fileName": "file B",
            "location": "logs/fileB",
            "size": "4034300",
            "lastUpdated": "May 01, 2015 01:21PM"
        },
        {
            "fileName": "file C",
            "location": "logs/fileC",
            "size": "53437000",
            "lastUpdated": "May 02, 2015 03:11PM"
        }
    ]
};

return {
    logs: getDummyLogsForNow()
};

In my controller, I use it as

$scope.logs = SupportService.logs;
$scope.selectedLogs = ["file C"];

and this is how I print them up on HTML table

    <table class="logsTable">
        <thead>
        <tr>
            <th>#</th>
            <th>File Name</th>
            <th>Last Updated</th>
            <th>Size</th>
            <th>Location</th>
        </tr>
        </thead>
        <tr ng-repeat="log in logs">
            <td>
                <input type="checkbox"
                       ng-checked="selectedLogs.indexOf(log.fileName) != -1"
                       ng-click="toggleSelect(log)">
            </td>
            <td>{{log.fileName}}</td>
            <td>{{log.lastUpdated}}</td>
            <td>{{log.size}}</td>
            <!--<td>{{log.location}}</td>-->
            <td>{{selectedLogs.indexOf(log.fileName) != -1}}</td>
        </tr>
    </table>

Even though one of the entries if true , the checkbox is not checked. All I see is

在此处输入图片说明

What's the issue here?

I think you may have something wrong with referencing your model. I've simplified your example (dropped the service) and have a working example here: https://jsfiddle.net/fpnte4td/6/

So probably there must be something in the controller or service... Can you try defining the sample data directly in the controller, like below?

$scope.logs = [{
        "fileName": "file A",
        "location": "logs/fileA",
        "size": "20000",
        "lastUpdated": "May 04, 2015 09:21PM"
    },{
        "fileName": "file B",
        "location": "logs/fileB",
        "size": "4034300",
        "lastUpdated": "May 01, 2015 01:21PM"
    },{
        "fileName": "file C",
        "location": "logs/fileC",
        "size": "53437000",
        "lastUpdated": "May 02, 2015 03:11PM"
    }
];

If this doesn't help, please post full body of the service and controller (skip irrelevant parts)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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