Rob*_*ini 34 javascript arrays function angularjs
我试图使用角度推动功能,但它不起作用.
我想将字符串(或对象)添加到数组中.
我在Stack Overflow上搜索了一些基本的例子,但我找不到它.
谁能纠正我的代码或写一个非常基本的例子?
这是我的例子.
这是HTML代码:
<form ng-controller="TestController as testCtrl ng-submit="testCtrl.addText(myText)">
<input type="text" value="Lets go">
<button type="button">Add</button>
</form>
Run Code Online (Sandbox Code Playgroud)
这是Java脚本代码:
(function() {
var app = angular.module('test', []);
app.controller('TestController', function() {
this.arrayText = {
text1: 'Hello',
text2: 'world',
}
this.addText = function(text) {
arrayText.push(this.text);
}
});
})();
Run Code Online (Sandbox Code Playgroud)
Ani*_*bhi 33
仅推送数组工作.
将arrayText对象设为Array Object.
试试这样
JS
this.arrayText = [{
text1: 'Hello',
text2: 'world',
}];
this.addText = function(text) {
this.arrayText.push(text);
}
this.form = {
text1: '',
text2: ''
};
Run Code Online (Sandbox Code Playgroud)
HTML
<div ng-controller="TestController as testCtrl">
<form ng-submit="addText(form)">
<input type="text" ng-model="form.text1" value="Lets go">
<input type="text" ng-model="form.text2" value="Lets go again">
<input type="submit" value="add">
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
Use*_*er2 12
请检查一下 - http://plnkr.co/edit/5Sx4k8tbWaO1qsdMEWYI?p=preview
控制器 -
var app= angular.module('app', []);
app.controller('TestController', function($scope) {
this.arrayText = [{text:'Hello',},{text: 'world'}];
this.addText = function(text) {
if(text) {
var obj = {
text: text
};
this.arrayText.push(obj);
this.myText = '';
console.log(this.arrayText);
}
}
});
Run Code Online (Sandbox Code Playgroud)
HTML
<form ng-controller="TestController as testCtrl" ng-submit="testCtrl.addText(testCtrl.myText)">
<input type="text" ng-model="testCtrl.myText" value="Lets go">
<button type="submit">Add</button>
<div ng-repeat="item in testCtrl.arrayText">
<span>{{item}}</span>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
小智 7
'推'代表数组.
你可以这样做:
app.js:
(function() {
var app = angular.module('myApp', []);
app.controller('myController', ['$scope', function($scope) {
$scope.myText = "Let's go";
$scope.arrayText = [
'Hello',
'world'
];
$scope.addText = function() {
$scope.arrayText.push(this.myText);
}
}]);
})();
Run Code Online (Sandbox Code Playgroud)
的index.html
<!doctype html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body>
<div>
<form ng-controller="myController" ng-submit="addText()">
<input type="text" ng-model="myText" value="Lets go">
<input type="submit" id="submit"/>
<pre>list={{arrayText}}</pre>
</form>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
//Comments object having reply oject
$scope.comments = [{ comment: 'hi', reply: [{ comment: 'hi inside commnet' }, { comment: 'hi inside commnet' }] }];
//push reply
$scope.insertReply = function (index, reply) {
$scope.comments[index].reply.push({ comment: reply });
}
//push commnet
$scope.newComment = function (comment) {
$scope.comments.push({ comment:comment, reply: [] });
}
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<!--Comment section-->
<ul ng-repeat="comment in comments track by $index" style="background: skyblue; padding: 10px;">
<li>
<b>Comment {{$index}} : </b>
<br>
{{comment.comment}}
<!--Reply section-->
<ul ng-repeat="reply in comment.reply track by $index">
<li><i>Reply {{$index}} :</i><br>
{{reply.comment}}</li>
</ul>
<!--End reply section-->
<input type="text" ng-model="reply" placeholder=" Write your reply." /><a href="" ng-click="insertReply($index,reply)">Reply</a>
</li>
</ul>
<!--End comment section -->
<!--Post your comment-->
<b>New comment</b>
<input type="text" placeholder="Your comment" ng-model="comment" />
<a href="" ng-click="newComment(comment)">Post </a>
</div>Run Code Online (Sandbox Code Playgroud)