2014-12-04 91 views
0

我的控制器後不更新:變量改變輸入

.controller('ExampleController', ['$scope', function($scope) { 
    $scope.barcode = 111; 
    $scope.scanBarcode = function() { 
     $scope.barcode = 123123123; 
    }; 

    $scope.scanThis = function() { 
     $scope.barcode = 456456456; 
    }; 
}]) 

我的觀點:

<form> 
    <div class="list list-inset"> 
     <label class="item item-input"> 
      <input type="number" placeholder="Code?" ng-model="barcode"> 
     </label> 
     {{barcode}} 
    </div> 
    <button class="button button-positive button-block" ng-click="scanBarcode()">Scan!</button> 
    <button class="button button-positive button-block" ng-click="scanThis()">Scan this!</button> 
</form> 

是作爲一個輸入和兩個按鈕來設置「條形碼」 VAR值一樣簡單。 它加載視圖時,我可以單擊按鈕和值的變化,但只要我改變輸入值的功能停止工作....

編輯1: 不,我沒有忘記「連接」視圖控制器,我有以下代碼:

.state('tab.dash', { 
     url: '/dash', 
     views: { 
     'tab-dash': { 
      templateUrl: 'templates/tab-dash.html', 
      controller: 'ExampleController' 
     } 
     } 
    }) 

編輯2: 如果我使用:

<form ng-controller="ExampleController"> 

它的工作原理,以及如果我用「$父「像這樣:

<input type="number" placeholder="Code?" ng-model="$parent.barcode"> 

這意味着使用「狀態」不工作如我所料...它開創了輸入「本地」範圍修改時...

+0

您是否在加載視圖時在輸入中看到「111」?如果不是,ExampleController範圍可能與表單元素不同(例如:如果表單是指令的一部分) – Edd 2014-12-04 13:46:33

+0

@Edd是的,我看到它。 – 2014-12-04 14:23:20

+1

如果您創建一個演示問題的Plunkr,它將會非常有幫助。 – 2014-12-04 14:28:45

回答

2

你忘了添加NG-控制器指示。

<form ng-controller="ExampleController"> 
    <div class="list list-inset"> 
     <label class="item item-input"> 
      <input type="number" placeholder="Code?" ng-model="barcode"> 
     </label> 
     {{barcode}} 
    </div> 
    <button class="button button-positive button-block" ng-click="scanBarcode()">Scan!</button> 
    <button class="button button-positive button-block" ng-click="scanThis()">Scan this!</button> 
</form> 
+0

不,請參閱我的編輯號碼1. – 2014-12-04 14:26:17

+0

但是,它的工作.. 。如果我在輸入上使用$ parent.barcode,它也能正常工作......這意味着使用「狀態」不能按預期工作...... – 2014-12-04 15:04:37

+0

use ng-model =「barcode」 – 2014-12-05 02:37:58