2016-10-31 104 views
0
{% load staticfiles %} 

<html ng-app="myBlog"> 
    <head> 
<!--  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script> 
--> <script type="text/javascript" src="{% static 'js/libs/angular.min.js' %}"></script> 
<!--  <script type="text/javascript" src="{% static 'js/modules/app.module.js' %}"></script> 
     <script type="text/javascript" src="{% static 'js/modules/app.config.js' %}"></script> --> 
<!--  <script type="text/javascript" src="{% static 'js/modules/blog-list.module.js' %}"></script> 
-->  <script type="text/javascript" src="{% static 'js/controllers/blog-list.js' %}"></script> 
    </head> 

    <body> 
     <input type="text" ng-model="name"> 
     <p>hi , {{name}}</p> 

     <div ng-controller="Controller"> 
      <h1>{{title}}</h1> 
      <button ng-click="someTest()">click</button> 
     </div> 
    </body> 
</html> 

這裏未定義是控制器

'use strict' 

var blogList = angular.module('myBlog' , []); 

blogList.controller('Controller', ['$scope', function($scope){ 

     $scope.clicks=""; 

     console.log("he"); 
     $scope.title = 'Hi there'; 
     $scope.clicks = 0; 
     console.log($scope) 

     $scope.someTest = function(){ 
      console.log($scope) 
      console.log("there") 
      $scope.clicks +=1; 
      $scope.title = 'Clicked' + clicks; 
     }; 
}]); 

當單擊該按鈕,它給出了錯誤

angular.min.js:118 ReferenceError: clicks is not defined at b.$scope.someTest (blog-list.js:16) at fn (eval at compile (angular.min.js:233), :4:215) at b (angular.min.js:126) at e (angular.min.js:276) at b.$eval (angular.min.js:145) at b.$apply (angular.min.js:146) at HTMLButtonElement. (angular.min.js:276) at Sf (angular.min.js:37) at HTMLButtonElement.d (angular.min.js:37)

我怎樣才能擺脫錯誤的。 。提前預訂

回答

0

您已使用$scope.title = 'Clicked' + clicks;。當您使用時,錯過了參考。將其更改爲$scope.title = 'Clicked' + $scope.clicks;所以說你的錯誤:

ReferenceError: clicks is not defined at b.$scope.someTest

$scope.someTest = function(){ 
    console.log($scope); 
    console.log("there"); 
    $scope.clicks += 1; 
    $scope.title = 'Clicked' + $scope.clicks; 
}; 
+0

標題沒有顯示在頁面 –

+0

@payalarora您是否已將'clicks'改爲'$ scope.clicks'? –

+0

是的,現在正在工作..但它不會顯示在index.html訪問使用{{title}} –

0

它似乎沒有定義變量 「點擊」

$ scope.title = '點擊' +點擊; < ---應該是「$ scope.clicks」吧?