angularjs – 在(one?)data-ng-init中声明两个对象文字并将它们绑定到不同的视图

我正在尝试创建两个不同的对象文字并将它们绑定到不同的视图(< table> s).但是,我不知道该怎么做.我已经尝试了下面的方法,我在父容器(div)的一个data-ng-init中声明了两个对象文字.我也尝试过为该div提供两个不同的data-ng-init指令,并在每个指令中都有一个对象文字.然而,这两种方法都不起作用,我得到错误(如果有人想看到它们会发布).

我试过的例子:

<div id="recipes" data-ng-init=" 
  dessertsdrinks = [
  {name: 'Apple Pie Popcorn',  url: 'pdfs/recipes/desserts_and_drinks/Apple Pie Popcorn.pdf'},  
  {name: 'Zucchini Muffins',  url: 'pdfs/recipes/Zucchini Muffins.pdf'} 
  ];

  maineats = [ 
  {name: 'Autumn Enchilada Casserole',  url: 'pdfs/recipes/Autumn Enchilada    Casserole.pdf'},
  {name: 'Build your own Taco',  url: 'pdfs/recipes/Build your own Taco.pdf'},]
  ">

  <table id="dessertsdrinks">
    <th>Desserts and Drinks</th>
        <tr data-ng-repeat="recipe in dessertsdrinks | filter:recipesearch | orderBy:'name'">
        <td><a href="{{ recipe.url }}"> {{ recipe.name }} </a></td>
        </tr>
   </table>

<table id="maineats">
        <th>Main Eats</th>
            <tr data-ng-repeat="recipe in maineats | filter:recipesearch | orderBy:'name'">
            <td><a href="{{ recipe.url }}"> {{ recipe.name }} </a></td>
            </tr>
       </table>
 </div>

当我只有一个对象文字和一个data-ng-init时,它可以很好地工作.
但是我怎么能用两个不同的列表呢?有没有更好的办法?

例如,我是否可以根据视图(表格)的id标签或其他东西将控件将对象文字绑定到$scope?像这样:

伪码

function recipeController(){
     if $scope id == "desertsdrinks" {
          $scope.recipes = [{.. dessert and drink recipe obj literal ..}];
     } else if $scope id == "maineats" {
          $scope.recipes =[{.. main eats recipe obj literal..}];
     }
 }

最佳答案 只需移动控制器内的对象即可:

$scope.dessertsdrinks = [
  {name: 'Apple Pie Popcorn',  url: 'pdfs/recipes/desserts_and_drinks/Apple Pie Popcorn.pdf'},  
  {name: 'Zucchini Muffins',  url: 'pdfs/recipes/Zucchini Muffins.pdf'} 
];

$scope.maineats = [ 
  {name: 'Autumn Enchilada Casserole',  url: 'pdfs/recipes/Autumn Enchilada    Casserole.pdf'},
  {name: 'Build your own Taco',  url: 'pdfs/recipes/Build your own Taco.pdf'}
]

无需更改在html中访问它们的方式.

点赞