2016-06-16 5 views
-1

私のコードを "controller as/this"構文から古典的な$ scope構文にコードを変換せずに変換しようとするときに重大な問題がありました。私は単純に "this"を$ scopeに置き換え、両方のコントローラの "controller as"割り当てを削除してみました。私はコントローラの/ this構文としてjsfiddleを作成していますので、構文を$ scopeに変換する前に正しく動作するはずです。 https://jsfiddle.net/6zk9vujo/6/ これは壊れたコードを表示するもう1つのjsfiffleです。これを$ scopeで置き換えて、コントローラをhtmlの割り当てとして削除します。https://jsfiddle.net/6zk9vujo/12/ありがとうございました。

HTMLテンプレートで

<div ng-app="app"> 
    <div ng-controller="mainController as main"> 
     <h2> 
    Main Controller 
    </h2> 
     <div> 
      <table> 
       <tr> 
        <td>Item</td> 
        <td>Price</td> 
        <td>Quantity</td> 
        <td></td> 
       </tr> 
       <tr ng-repeat="product in main.items"> 
        <td>{{product.name}}</td> 
        <td>{{product.price | currency}}</td> 
        <td> 
         <button ng-click="main.increaseItemAmount(product)"> 
          + 
         </button> 
         {{product.quantity}} 
         <button ng-click="main.decreaseItemAmount(product)"> 
          - 
         </button> 
         <button ng-click="main.addToCart(product)"> 
          Add to Cart 
         </button> 
        </td> 
       </tr> 
      </table> 
     </div> 
    </div> 
    <div ng-controller="cartController as cart"> 
     <h2> 
    Cart Controller 
    </h2> 
     <div> 
      <table> 
       <tr> 
        <td>Item</td> 
        <td>Price</td> 
        <td>Quantity</td> 
        <td></td> 
       </tr> 
       <tr ng-repeat="product in cart.cartStorage.items"> 
        <td>{{product.name}}</td> 
        <td>{{product.price | currency}}</td> 
        <td> 
         <button ng-click="cart.increaseItemAmount(product)"> 
          + 
         </button> 
         {{product.quantity}} 
         <button ng-click="cart.decreaseItemAmount(product)"> 
          - 
         </button> 
         <button ng-click="cart.removeFromCart(product)"> 
          Remove from Cart 
         </button> 
        </td> 
       </tr> 
      </table> 
     </div> 
    </div> 
</div> 

JAVASCRIPT

angular.module('app', []) 
.factory('cartStorage', function() { 
    var _cart = { 
     items: [] 
    }; 
    var service = { 
     get cartItems() { 
      return _cart; 
     } 
    } 
    return service; 
}) 
.controller('mainController', function(cartStorage) { 
    var _this = this; 
    _this.cartStorage = cartStorage.cartItems; 

    _this.items = [{ 
     name: 'Apple', 
     price: 2.5, 
     quantity: 1 
    }]; 

    _this.addToCart = function(product) { 
     _this.cartStorage.items.push(product); 
     product.addedToCart = true; 
    } 

    _this.increaseItemAmount = function(product) { 
     product.quantity++; 
     product.showAddToCart = true; 
    } 

    _this.decreaseItemAmount = function(item) { 
     product.quantity--; 
     if (product.quantity <= 0) { 
      product.quantity = 0; 
      product.addedToCart = false; 
      product.showAddToCart = false; 
      var itemIndex = _this.cartStorage.items.indexOf(product); 
      if (productIndex > -1) { 
       _this.cartStorage.items.splice(productIndex, 1); 
      } 
     } else { 
      product.showAddToCart = true; 
     } 
    } 
}) 
.controller('cartController', function(cartStorage) { 
    var _this = this; 
    _this.cartStorage = cartStorage.cartItems; 

    _this.increaseItemAmount = function(item) { 
     product.quantity++; 
    } 

    _this.decreaseItemAmount = function(item) { 
     item.quantity--; 
     if (item.quantity <= 0) { 
      item.quantity = 0; 
      item.addedToCart = false; 
      item.showAddToCart = false; 
      var productIndex = _this.cartStorage.items.indexOf(item); 
      if (productIndex > -1) { 
       _this.cartStorage.items.splice(productIndex, 1); 
      } 
     } 
    } 

    _this.removeFromCart = function(item) { 
     item.quantity = 0; 
     item.addedToCart = false; 
     item.showAddToCart = false; 
     var productIndex = _this.cartStorage.items.productOf(item); 
     if (productIndex > -1) { 
      _this.cartStorage.items.splice(productIndex, 1); 
     } 
    } 
}); 
+0

あなたはおそらくあなたが最初の場所に変換する必要がありますなぜあなたは – Phil

+1

を持っていたものの問題述べるべきですか? – charlietfl

+0

@charlietfl、私のアプリケーションの現在のコードベースは "コントローラとして/ this"の構文ではなく、このコードを使用することは、私のコードの残りの部分と統合しながら、私は一日のためにそれに取り組んできた。私のコードベースはどこでも古典的な$スコープを使用しています。 – user791134

答えて

0

、すべてのmain.cart.を削除し、ng-controller="mainController"ng-controller="cartController"に変更します。

コントローラでは、$scopeを注入し、最も簡単な移行のために_thisに割り当てます。

.controller('mainController', function($scope, cartStorage) { 
    var _this = $scope; 

.controller('cartController', function($scope, cartStorage) { 
    var _this = $scope; 

https://jsfiddle.net/6zk9vujo/10/


また、ちょうどあなたのコントローラで$scopeを持つすべての_thisの参照を交換してください。

また、product/itemproductIndex/itemIndexという変数が混在しています。私はこれらすべてをこのフィドルで標準化し、同じ製品を再追加するというロジックを修正しました。

https://jsfiddle.net/6zk9vujo/13/

+0

私はあなたの助けを感謝Phil、しかし、私はこのコードで完全にキーワード "this"を取り除き、 "$ scope"を使用して、構文が流行したのでcontorollerの前に存在していた古典的なAngularJSスタイルを使用しようとしています。たとえば、それを読むのではなく、 "_this.addToCart = function(product){...."} $ scope.addToCart = function(product){.... "キーワード "this"、 "vm"、または "controller as"を使わずに$ scopeを表示するにはjsfiddle?ありがとう。 – user791134

+0

@ user791134なぜですか? '_this'は単なる変数であり、決して' this'(コントローラのインスタンス)には関係しません。もしあなたがそうしなければならない場合は、あなたのコントローラーで '_this'を' $ scope'に検索/置き換えてください。 – Phil

+0

Phil。私たちが議論したように、すべてのリファクタリングされたこの新しいjsfiddleを見てください。 https://jsfiddle.net/6zk9vujo/12/コードが壊れています。ありがとう。 – user791134

-1

ビューでコントローラを定義するときに、構文「として」を削除した場合には動作します:ng-controller="mainController"ng-controller="cartController"

編集:誤ったフィドルリンクを間違えてしまった。

https://jsfiddle.net/analiza641/jr0stbLq/3/

+1

私はあなたの入力Alexに感謝しますが、それは私の問題を解決しません。私はすでにこれを試して、このスコープを$ scopeに置き換えて、コードを壊してしまったので、なぜここで助けを求めているのですか?また、あなたのjsfiddleは私のものと全く同じであり、変更を表示していません。再度、感謝します。 – user791134

+0

なぜ私はそれを指摘してくれてありがとう、なぜdownvotedされたのだろうか。私は私の答えの草案を保存していないようだ。私はリンクを更新したが、それは遅すぎると思う。とにかくありがとう。 –

+0

問題ありません。ちょうどそうあなたは私がdownvoteをした人ではなかったことを知っている!私は個人的には、その理由を指摘することなく、私の質問/回答をここでダウンv投票するのが好きではないことを認めます。それにかかわらず、コーディングを続けてください! :) – user791134

関連する問題