2014-12-05 62 views
0

想象一下,我們有一些產品清單。我們通過PHP獲取它們。例如:同步角項目(產品)

$products = Product::all(); 

然後在我們看來:

<?php foreach ($products as $product): ?> 
    <div class='product'> 
     <div class="name"><?php echo $product->name ?></div> 
     <div class="short-info"><?php echo $product->short ?></div> 

     <div class="quantity"> 
      <button class="add-one">-</button> 
      <input type="text" value="<?php echo $product->quantity ?>"> 
      <button class="remove-one">+</button> 
     </div> 
    </div> 
<?php endforeach ?> 

在角度,我們都會有一些這樣想:

<div class='product' ng-repeat="product in products"> 
    <div class="name">{{ product.name }}</div> 
    <div class="short-info">{{ product.short }}</div> 

    <div class="quantity"> 
     <button class="add-one" ng-click="removeOne(product)">-</button> 
     <input type="text" ng-model="{{ product.quantity }}"> 
     <button class="remove-one" ng-click="addOne(product)">+</button> 
    </div> 
</div> 

但是我們怎麼能讓這些東西一起工作? 我想用php(對於seo puproses)立即填充products,並具有角度提供的那些功能。

**EXAMPLE:** 
User navigate to http://.../catalog 
User instantly see product list (because they were rendered with php) 
User instantly can change product amount (here comes angular ng-model) 

如何做到這一點?由於

回答

0

您可以使用

<div class='product' ng-repeat="product in products" ng-init="products=<?php echo json_encode($products);?>"> 
    <div class="name">{{ product.name }}</div> 
    <div class="short-info">{{ product.short }}</div> 

    <div class="quantity"> 
     <button class="add-one" ng-click="removeOne(product)">-</button> 
     <input type="text" ng-model="{{ product.quantity }}"> 
     <button class="remove-one" ng-click="addOne(product)">+</button> 
    </div> 
</div> 

我不是PHP程序員,但你可以在這裏得到的想法。

+0

但是當您查看頁面源代碼(http://take.ms/626VU)時,將只有角色模板(不是渲染產品) – user2573863 2014-12-05 12:20:07

+0

如果您正確安裝使用ng-app的角度模塊並使用控制器,則指令ng -init相當於在控制器中聲明產品列表。所以在頁面上將是具有正確綁定的產品列表。 – Mikalai 2014-12-05 21:36:20

+0

你可以自己測試一下,看看它不是。在「Cmd + opt + u」中,您將看不到呈現的產品,因爲它們通過js – user2573863 2014-12-06 12:06:59