【问题标题】:ng-repeat and selected checkboxng-repeat 并选中复选框
【发布时间】:2018-07-19 22:35:42
【问题描述】:

我有一个复选框列表,我需要根据 PHP 数组将其标记为选中

<?
    $selected ['1', '100', '250'];
?>

<script>
 $scope.articles = [
    "1",
    "2",
    "100",
    "250",
    "500"
]
</script>

<ul>
    <div ng-repeat="article in articles">
        <input type="checkbox" ng-checked="?" id="{{article}}">
    </div>
</ul>

问题是我不知道如何将 php 数组与 angularjs 部分“连接”。我可以回显 php 数组,但不确定如何将其传递给 JS

谢谢。

【问题讨论】:

  • 为什么不做一个服务(PHP),它会告诉你信息(['1', '100', '250']),从角度$http.get调用它,(从这里太容易了)更新值相应地在视图中?
  • 请不要这样做,创建一个服务并从后端检索该数组。

标签: javascript php angularjs


【解决方案1】:

同意@Ele 的评论,你应该在 angularjs 服务中拥有它,从后端获取它。在您的服务中有一个方法,将 article.Id 传递给它,该服务将检查传递的 id 是否存在于数组中,基于您的复选框将被设置。

模板:

<div ng-repeat="article in articles">
   <input type="checkbox" ng-checked="isArticlePresent(article.Id)" id="{{article}}">
</div>

控制器

$scope.isArticlePresent = function(id){
return service.isArticlePresent(id);
};

服务

this.isArticlePresent = function (id) {
    //Write your logic, and return true/false
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 2015-08-29
    • 2015-09-09
    • 2013-01-27
    • 1970-01-01
    相关资源
    最近更新 更多