2016-12-06 10 views
0

私は通常、昼食のために食べるコンマで区切られた項目をリストすることができるテキストボックスをユーザに提示するフロントエンドアプリケーションを作成しようとしています。これが入力されたら、ユーザーは「Check If Too Much」ボタンをクリックする必要があります。角型が認識されません。プログラムが動作しない

私は初心者だけど、まだ角の偉大な把握を持っていません。私のプログラムは動作しません。 1つは、式をHTMLで補間するときに式が認識されないということです。理由は分かりません。

私はそこに私のディレクティブの配置と間違って何かあってもよいが、実際にはわからない、実際にいくつかの助けを使用することができかもしれないと思います。

(function() { 
 
'use-strict'; 
 

 
angular.module('lunchCheck', []) 
 
    .controller('LunchCheckController', function ($scope) { 
 

 

 
    $scope.numberitems = function() { 
 
     var itemstring = $scope.items 
 
     var lunchItems = itemstring.split(",") 
 
     return length.lunchItems 
 
    }; 
 

 
    $scope.statement = function() { 
 
     if ($scope.numberitems==0) { 
 
     return "Please enter data first!" 
 
     } 
 
     else if (0<$scope.numberitems<5) { 
 
     return "Enjoy!" 
 
     } 
 
     else if ($scope.numberitems>4) { 
 
     return "Too much!" 
 
     } 
 
    }; 
 

 
    }); 
 

 
})();
<!doctype html> 
 
<html lang="en" ng-app="lunchCheck"> 
 
    <head> 
 
    <title>Lunch Checker</title> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <link rel="stylesheet" href="styles/bootstrap.min.css"> 
 
    <style> 
 
     .message { font-size: 1.3em; font-weight: bold; } 
 
    </style> 
 
    </head> 
 
<body> 
 
    <div class="container" ng-controller="LunchCheckController"> 
 
    <h1>Lunch Checker</h1> 
 

 
     <div class="form-group"> 
 
      <input id="lunch-menu" type="text" 
 
      placeholder="list comma separated dishes you usually have for lunch" 
 
      class="form-control" 
 
      ng-model="items"> 
 
     </div> 
 

 
     <div class="form-group"> 
 
      <button 
 
      class="btn btn-default" 
 
      ng-click="statement">Check If Too Much</button> 
 
     </div> 
 

 
     <div class="form-group message" ng-click="statement"> 
 
      {{statement}} 
 
     </div> 
 
    </div> 
 

 
</body> 
 
</html>

+0

どこページの 'angular.js'参照がありますか? –

+0

文は関数なので、関数として '()'を使う必要があります。 – Nilesh

答えて

0

両方$scope.numberitems$scope.statement関数として定義されます。結果を得るために関数を実行する必要があります。まず、numberitems関数を呼び出して、$scope.statement以内でカウントを取得します。

$scope.statement = function() { 
    var numberOfItems = $scope.numberitems() 

    if (numberOfItems===0) { 
    return "Please enter data first!" 
    } 
    else if (numberOfItems > 0 && numberOfItems < 5) { 
    return "Enjoy!" 
    } 
    else if (numberOfItems > 4) { 
    return "Too much!" 
    } 
}; 

次に、ビューで関数を呼び出します。

{{ statement() }}

これはテストされていないですが、そのようなことは、近いあなたを取得する必要があります。

0

あなたが示したコードにはさまざまな問題があります。

はNGクリックで函数を呼び出していない:ng-clickため 、のfuctionはng-click="statement()"のように起動する必要があります。

長さ。がする機能を作成します。0ここに機能し、値として文を使用して

違いはありません:numberitems機能に:(0 <は$ scope.numberitems < 5)があれば、他のlunchItems.length

に変更し変数内の値を更新し、その変数を使用して変数を表示します。

私も同じように、あなたの変数を初期化することをお勧めしたい:

.controller('LunchCheckController', function ($scope) { 
    $scope.statement = ''; 
    $scope.items = '';` 

私はなって何とフィドルを作った期待された結果だった:

click for fiddle

0

あなたのコードを使用し、いくつかの変更を加えました。

<!doctype html> 
 
<html lang="en" ng-app="lunchCheck"> 
 

 
<head> 
 
    <title>Lunch Checker</title> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <link rel="stylesheet" href="styles/bootstrap.min.css"> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.9/angular.js"></script> 
 
    <style> 
 
    .message { 
 
     font-size: 1.3em; 
 
     font-weight: bold; 
 
    } 
 
    </style> 
 
</head> 
 

 
<body> 
 
    <div class="container" ng-controller="LunchCheckController"> 
 
    <h1>Lunch Checker</h1> 
 

 
    <div class="form-group"> 
 
     <input id="lunch-menu" type="text" placeholder="list comma separated dishes you usually have for lunch" class="form-control" ng-model="items"> 
 
    </div> 
 

 
    <div class="form-group"> 
 
     <button class="btn btn-default" ng-click="statement()">Check If Too Much</button> 
 
    </div> 
 

 
    <div class="form-group message"> 
 
     {{ istoomuch }} 
 
    </div> 
 
    </div> 
 
    <script> 
 
    (function() { 
 
     'use-strict'; 
 

 
     angular.module('lunchCheck', []) 
 
     .controller('LunchCheckController', function($scope) { 
 
      $scope.istoomuch = ''; 
 

 
      $scope.numberitems = function() { 
 
      if ($scope.items) { 
 
       var itemstring = $scope.items 
 
       var lunchItems = itemstring.split(",") 
 
       return lunchItems.length; 
 
      } else { 
 
       return 0; 
 
      } 
 
      }; 
 

 
      $scope.statement = function() { 
 
      var totalitems = $scope.numberitems(); 
 
      console.log(totalitems); 
 
      if (totalitems === 0) { 
 
       $scope.istoomuch = "Please enter data first!" 
 
      } else if (totalitems < 5) { 
 
       $scope.istoomuch = "Enjoy!" 
 
      } else if (totalitems > 4) { 
 
       $scope.istoomuch = "Too much!" 
 
      } 
 
      }; 
 

 
     }); 
 

 
    })(); 
 
    </script> 
 

 
</body> 
 

 
</html>

関連する問題