根据单选按钮选择添加文本(ANGULAR)

时间:2016-06-25 14:46:57

标签: javascript angularjs radio-button

我有单选按钮,选中后我想将输入框中的文本添加到相应部分的ul(作为列表项)

<body ng-app="MyApp">
    <div ng-controller="MyController as myCtrl">
        <div class='wrap'>
                <p>Section 1<p>
                    <ul>
                        <li ng-repeat="item in myCtrl.items1">{{item}}</li>
                    </ul>
                <p>Section 2<p>
                    <ul>
                        <li ng-repeat="item in myCtrl.items2">{{item}}</li>
                    </ul>
                <p>Section 3<p>
                    <ul>
                        <li ng-repeat="item in myCtrl.items3">{{item}}</li>
                    </ul>
        </div>
        <div class='wrap'>
            File Name: <input type="text" ng-model="myCtrl.fileName">
            <button ng-click="myCtrl.addFile()">Add File</button>
            <div>
                <input type="radio" name="foldertoadd" ng-value="myCtrl.section1" ng-model="myCtrl.sectionSelected"> Section 1
                <input type="radio" name="foldertoadd" ng-value="myCtrl.section2" ng-model="myCtrl.sectionSelected"> Section 2
                <input type="radio" name="foldertoadd" ng-value="myCtrl.section3" ng-model="myCtrl.sectionSelected"> Section 3
            </div>
        </div>
    </div>
</body><!-- end of MyApp, angular ends here -->

这是我的控制器。到目前为止,我已将其设置为从输入框中获取文本并将其推送到第1节的ul。但是我希望用户能够选择一个单选按钮来选择该部分,然后将输入文本推送到该部分的ul

var myModule = angular.module("MyApp", []);

myModule.controller('MyController', function(){

    var self = this;

    self.items1 = ["File 1.1","File 1.2","File 1.3"];
    self.items2 = ["File 2.1","File 2.2","File 2.3"];
    self.items3 = ["File 3.1","File 3.2","File 3.3"];

    self.sectionSelected = false;

    self.addFile = function() {
        var textAdded = self.fileName;
        self.items1.push(textAdded);
    }

});

我还设置了一个小提琴,但无法加载角度: https://jsfiddle.net/RL_NewtoJS/tx7novnb/10/

1 个答案:

答案 0 :(得分:1)

我让它工作https://jsfiddle.net/tx7novnb/13/

问题是无线电按钮的ng值:

<input type="radio" name="foldertoadd" ng-value="myCtrl.item1" ng-model="myCtrl.sectionSelected"> Section 1

self.addFile = function() {
    var textAdded = self.fileName;
    self.sectionSelected.push(textAdded);
}