2014-09-04 45 views
-2

我想要做的是使用角度將每個用戶輸入轉換爲標籤。我相信我做了正確的事情,但沒有成功。如果有人看一下這段代碼,我會很感激。謝謝使用angular.js將用戶輸入轉換爲標籤

這裏是一個plunker

<div class="row" ng-controller="tagForm"> 
    <div ng-click="addEntry()"> 
    <div class="col-xs-12 col-sm-12 col-md-10 "> 
     <input type="text" placeholder="What are your area of expertise" ng-model="newEntry.name" class="form-control border" /> 
    </div> 
    <div class="col-xs-12 col-md-2 center form-button"> 
     <input type="button" value="Add" class="btn btn-orange btn-add" /> 
    </div> 
    <div class="col-md-8 col-sm-offset-2" id="up"> 
     <br /> 
     <span class="label label-primary" ng-repeat="entry in entries">{{entry.name}}</span> 
    </div> 
    </div> 
</div> 

app.controller('tagForm', ['$scope', function($scope) { 
    return $scope.addEntry = function() { 
    $scope.entries.push($scope.newEntry); 
    return $scope.newEntry = {}; 
}; 
}]); 
+1

請問你能解釋一下嗎?即你想將每個轉換爲標籤? – BlaShadow 2014-09-04 16:41:01

+0

我試圖做的是,當用戶在輸入框中鍵入一個單詞並單擊添加時,它會將該單詞轉換爲標籤。像這樣的http://aehlke.github.io/tag-it/ – 2014-09-04 17:07:03

回答

1

你有幾件事錯在你普拉克,但這裏的一些東西開始:

  1. 您需要連線點擊事件在你的添加按鈕上。現在,它沒有做任何事情,當你點擊它

  2. 綁定到剛剛使用'newEntry'範圍的ng模型。你所輸入的只是一個名字,所以你只需要保存在示波器上即可。

  3. 遍歷條目,打印出只,而不是 'entry.name'

而且您的控制器 '條目' 應該是這樣的(無需回報)

app.controller('tagForm', ['$scope', function($scope) { 
    $scope.entries = []; 
    $scope.addEntry = function() { 
     $scope.entries.push($scope.newEntry); 
    }; 
}]); 

看到這個小提琴:

http://jsfiddle.net/smaye81/anrv2qms/1/

+0

抱歉。我嘗試你的建議,但仍然無法正常工作。 http://plnkr.co/edit/MNdVk6 – 2014-09-04 17:03:58

+1

編輯我的答案,並附上工作小提琴的鏈接 – sma 2014-09-04 17:28:50

+0

非常感謝您的幫助... – 2014-09-04 17:49:15

相關問題