Angular -在循环中将字符串连接到$scope

Angular - concatenate string to $scope in loop

本文关键字:连接 scope 字符串 循环 Angular      更新时间:2023-09-26

我有四个值的名字:

$scope.restaurant.restaurantIsTop
$scope.restaurant.restaurantIsRecommended
$scope.restaurant.restaurantIsNew 
$scope.restaurant.restaurantIsPromoted

可以是0或1。我想检查它们的值是否等于1,将其改为true,否则它将为false。

我可以用这个方法对四个变量做这个:

        if ($scope.restaurant.restaurantIsTop == 1  ?
                $scope.restaurant.restaurantIsTop = true :
                $scope.restaurant.restaurantIsTop = false);
        if ($scope.restaurant.restaurantIsNew == 1  ?
                $scope.restaurant.restaurantIsNew = true :
                $scope.restaurant.restaurantIsNew =false);
        if ($scope.restaurant.restaurantIsRecommended == 1  ?
                $scope.restaurant.restaurantIsRecommended = true :
                $scope.restaurant.restaurantIsRecommended =false);
        if ($scope.restaurant.restaurantIsPromoted == 1  ?
                $scope.restaurant.restaurantIsPromoted = true :
                $scope.restaurant.restaurantIsPromoted =false);  

绝对有效,但它不是多值的有效方法。所以我创建了一个数组:

var varietyArray = ["restaurantIsTop" ,
                                "restaurantIsRecommended",
                                "restaurantIsNew",
                                "restaurantIsPromoted"
            ];
        angular.forEach (varietyArray, function (val) {
            console.log($scope.restaurant.val);
        })

我想检查上面循环中的四个变量。有什么建议吗?

EDITED:

我想在Angular Material复选框中显示它们:

                    <div class="col-sm-12">
                            <div class="md-form-group">
                                <md-checkbox ng-model="restaurant.restaurantIsTop ">
                                    best
                                </md-checkbox>
                                <md-checkbox ng-model="restaurant.restaurantIsRecommended">
                                    suggested
                                </md-checkbox>
                                <md-checkbox ng-model="restaurant.restaurantIsNew">
                                    new
                                </md-checkbox>
                                <md-checkbox ng-model="restaurant.restaurantIsPromoted">
                                    promote
                                </md-checkbox>
                            </div>
                    </div>

要检查循环中的变量并有条件地设置它,您可以这样做:

var $scope = {
  restaurantIsTop: 1,
  restaurantIsRecommended: 0,
  restaurantIsNew: 0,
  restaurantIsPromoted: 1
};
var varietyArray = ["restaurantIsTop" ,
                            "restaurantIsRecommended",
                            "restaurantIsNew",
                            "restaurantIsPromoted"
        ];
angular.forEach (varietyArray, function (val) {
  $scope[val] = ($scope[val]) ? false : true;
  console.debug($scope[val]);
})

您可以使用$scope[VariablenameAsString]

访问作用域变量

jsfiddle

正如有人已经评论的那样,你不需要改变这些属性的值。0等于假,1等于真。

Material复选框应该将这些值解释为"false"或"true",所以我认为至少在通过复选框呈现数据时应该没问题。

尽管如此,请记住,复选框将在模型(属性)中"写入"适当的bool值,当它们改变时,您可能最终得到混合的0,1,true, false值。

希望这对你有帮助!