【问题标题】:Angularjs: Use ng-repeat="(key, value) in myObj" to show data from jsonAngularjs:使用 ng-repeat="(key, value) in myObj" 显示来自 json 的数据
【发布时间】:2016-12-29 19:34:04
【问题描述】:

我正在创建一个通知应用程序,它将通过电子邮件和短信发送通知。启动时将设置为 true(无法更改 json 数据)

我创建了切换按钮,它将每封电子邮件和短信的状态更改为真假。当状态为真假时,需要再次发送json数据,格式如下:

PUT : http://example.url
{
    "category": "Fruit",
    "method": "EMAIL",
    "enabled": true
}

HTML:

<div class="notification-inner-wrap" ng-repeat="settings in pgxNotification.preferences | orderBy:'order'" >
    <p class="notification-heading">{{settings.code}}</p>
    <div ng-repeat="(key, value) in pgxNotification.preferences">
        <div class="notification-methods">
            <span>{{settings.methods[0]}}</span>
            <div class="notification-on-off-icon">
                <i class="fa fa-toggle-on active" ng-if="status == true" ng-click="status = !status"></i>
                <i class="fa fa-toggle-on fa-rotate-180 inactive" ng-if="status == false" ng-click="status = !status"></i>
            </div>
            <pre>{{ status }}</pre>
        </div>
        <div class="notification-methods">
            <span>{{settings.methods[1]}}</span>
            <div class="notification-on-off-icon">
                <i class="fa fa-toggle-on active" ng-if="status == true" ng-click="status = !status"></i>
                <i class="fa fa-toggle-on fa-rotate-180 inactive"  ng-if="status == false" ng-click="status = !status"></i>
            </div>
            <pre>{{ status }}</pre>
        </div>
    </div>
</div>

JS:

app.controller('MainCtrl', function($scope) {
  $scope.status = true;
        $scope.changeStatus = function(status){
            $scope.status = !$scope.status;
            //var method = $scope.pgxNotification.methods[index];

        }


  var response = {
        "status" : true,
        "exception" : null,
        "data": {
            "methods": ["SMS","EMAIL","PUSH"],
            "preferences": [
                {
                    "code": "Fruit",
                    "name": "Fruit content",
                    "methods": ["SMS", "EMAIL"]
                },
                {
                    "code": "Vegetable",
                    "name": "Vegetable content",
                    "methods": ["SMS", "EMAIL"]
                },
                {
                    "code": "Car",
                    "name": "Car content",
                    "methods": ["SMS", "EMAIL"]
                },
                {
                    "code": "bike",
                    "name": "bike content",
                    "methods": ["SMS", "EMAIL"]
                }
            ]
        }
        };
  $scope.pgxNotification = response.data;

});

这里是plunkr:

https://plnkr.co/edit/pIBjao0ujuAzyJGZSPfU?p=preview

现在开关按钮不起作用,需要弄清楚如何在开关上发送上述格式的json响应。

我在某处读到需要使用ng-repeat="(key, value) in pgxNotification.preferences" 设置键值,然后发送 json 响应。

【问题讨论】:

    标签: angularjs json angularjs-ng-repeat


    【解决方案1】:

    您在控制器范围级别使用status 变量。因此,来自任何控件的status 上发生的任何最后更新都将是最终视图值。因此,您没有得到预期的输出。

    Here is the updated plunkr

    【讨论】:

    • 我只看到现在,一个切换按钮状态正在更改为 on 和 off 但无法捕获值 true 或 false 以发送 json 响应,在上面的 put 方法中。
    • 好的,我现在可以更改值了,但是你能解释一下如何使用 put 方法发送 json 响应吗?我是 Angular 的新手,所以......我改变了哪个按钮切换我需要发送 json 响应。
    • 你的技术有问题,我不想将value初始化为true,没有它我们能做到吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    相关资源
    最近更新 更多