在PHP中,你可以做...
range(1, 3); // Array(1, 2, 3)
range("A", "C"); // Array("A", "B", "C")
Run Code Online (Sandbox Code Playgroud)
也就是说,有一个函数可以通过传递上限和下限来获取一系列数字或字符.
是否内置了JavaScript本身的内容?如果没有,我将如何实施它?
嵌套在我们的Angular应用程序中的是一个名为Page的指令,由一个控制器支持,该控制器包含一个带有ng-bind-html-unsafe属性的div.这被分配给名为'pageContent'的$ scope var.此var从数据库中分配动态生成的HTML.当用户翻转到下一页时,会调用DB,并将pageContent var设置为这个新的HTML,它将通过ng-bind-html-unsafe在屏幕上呈现.这是代码:
页面指令
angular.module('myApp.directives')
.directive('myPage', function ($compile) {
return {
templateUrl: 'page.html',
restrict: 'E',
compile: function compile(element, attrs, transclude) {
// does nothing currently
return {
pre: function preLink(scope, element, attrs, controller) {
// does nothing currently
},
post: function postLink(scope, element, attrs, controller) {
// does nothing currently
}
}
}
};
});
Run Code Online (Sandbox Code Playgroud)
Page指令的模板(上面的templateUrl属性中的"page.html")
<div ng-controller="PageCtrl" >
...
<!-- dynamic page content written into the div below -->
<div ng-bind-html-unsafe="pageContent" >
...
</div>
Run Code Online (Sandbox Code Playgroud)
页面控制器
angular.module('myApp') …Run Code Online (Sandbox Code Playgroud) 如何使用NG-重复类似的 Javascript中?
例:
<div ng-repeat="4">Text</div>
Run Code Online (Sandbox Code Playgroud)
我想用ng-repeat迭代4次,但我该怎么做呢?
我试图ng-repeat在一个div应该包含星形图像的地方使用,JSON中的每个饼都有rating1-5 的属性,我想用这个值来圈出x个星星.我有点工作,但它的方式存在缺陷,我无法对数组进行重新排序,并使星星跟随列表中的正确项目,因为我[$index]用来跟踪迭代.
我的解决方案也相当丑陋,因为我创建的数组具有与rating属性值一样多的索引占位符,然后将其推入数组以循环出适当数量的图像.我想有一个更优雅的解决方案.
如何在不使用的情况下解决这个问题[$index]?
JSON的片段:
{"pies": [
...
{
"name": "Blueberry pie",
"imageUrl": "img/blueberrypie.png",
"id": "1",
"rating": "5", //Ng-repeat depending on this value
"description": "Blueberry pie is amazing."
},
...
]}
Run Code Online (Sandbox Code Playgroud)
我的控制器:
pieShopApp.controller('shopCtrl', ['$scope', '$http', '$routeParams', function ($scope, $http, $routeParams) {
$scope.pieId = $routeParams.pieId,
$scope.sortingOptions = ['A-Z', 'Rating'],
$scope.sortingValues = ['name', 'rating'],
$scope.ratings = [],
$http.get('jsons/pies.json')
.success(function(data, status) {
$scope.pies = data;
for (i = 0; i < …Run Code Online (Sandbox Code Playgroud) 我有一个包含int属性的JSON对象x,我想重复代码x次
<span class="glyphicon glyphicon-star"/>
Run Code Online (Sandbox Code Playgroud)
ng-repeat似乎没有显示,因为它正在与集合一起使用.
任何建议(角新手)
使用Angularjs,<div>如果用户在文本框中输入"10" ,我想创建say 10 .
<input type="text" id="gcols" placeholder="Number of Columns" ng-model="cols"/>
Run Code Online (Sandbox Code Playgroud)
因此,无论用户在此框中输入什么值,都应创建许多值.所以我的一般性问题是,如何'ng-repeat' <div>为'ng-model'时代?
更新: 感谢您的所有答案,我通过引用您的答案做了类似的事情.这是现在的工作,但告诉我,是否有任何其他逻辑比这更有效.
$scope.divs = new Array();
$scope.create=function(){ //I added a button & invoked this function on its ng-click
var a = $scope.cols;
for(i=0;i<a;i++)
{
$scope.divs.push(a);
}
alert($scope.divs);
};
Run Code Online (Sandbox Code Playgroud) 我想使用angularJS绑定将html表行绑定特定的计数时间,如下所示:
<table>
<tr ng-repeat="x in 5">
<td>abc</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
如果有人有解决方案,请尽可能回复.谢谢
我有嵌套ng-repeat像这样的东西.我想将它限制为仅使用列表中的前两项(而不是完整列表).
ng-repeat = "items in phone.comments
Run Code Online (Sandbox Code Playgroud)
和HTML:
<div class="wallList clearfix" ng-repeat = "items in phone.comments | limitTo:quantity">
<div class="wallListImage">
<a href="#">
<img ng-show = "items.imageUrl" ng-src="{{items.imageUrl}}" height = "42px" width= "42px"/>
<img ng-show = "!items.imageUrl" ng-src="WishlistImage/movies.png" height = "42px" width= "42px"/>
</a>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)