小编Din*_*ine的帖子

角度ng重复错误"不允许在转发器中重复".

我正在定义一个自定义过滤器:

<div class="idea item" ng-repeat="item in items" isoatom>    
    <div class="section comment clearfix" ng-repeat="comment in item.comments | range:1:2">
        ....
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的那样,使用过滤器的ng-repeat嵌套在另一个ng-repeat中

过滤器的定义如下:

myapp.filter('range', function() {
    return function(input, min, max) {
        min = parseInt(min); //Make string input int
        max = parseInt(max);
        for (var i=min; i<max; i++)
            input.push(i);
        return input;
    };
});
Run Code Online (Sandbox Code Playgroud)

我越来越:

错误:不允许在转发器中重复.Repeater:在item.comments中发表评论| 范围:1:2 ngRepeatAction @ https://ajax.googleapis.com/ajax/libs/angularjs/1.1.4/an

angularjs ng-repeat

466
推荐指数
4
解决办法
31万
查看次数

如何将CSS3过渡应用于除background-position之外的所有属性?

我想将CSS转换应用于除background-position之外的所有属性.我试着这样做:

.csstransitions a {
    -webkit-transition: all 0.3s ease;                  
    -moz-transition: all 0.3s ease;                 
    -o-transition: all 0.3s ease;   
    -ms-transition: all 0.3s ease;          
    transition: all 0.3s ease;
}

.csstransitions a {
    -webkit-transition: background-position 0s ease 0s;                 
    -moz-transition: background-position 0s ease 0s;                
    -o-transition: background-position 0s ease 0s;  
    -ms-transition: background-position 0s ease 0s;         
    transition: background-position 0s ease 0s;
}
Run Code Online (Sandbox Code Playgroud)

首先,我将所有属性设置为转换,然后我尝试仅覆盖background-position属性的转换.

然而,这似乎也重置了所有其他属性 - 所以基本上没有任何过渡似乎再发生.

有没有办法在不列出所有属性的情况下执行此操作?

css3 css-transitions

98
推荐指数
3
解决办法
10万
查看次数

如何将模型插入特定索引中的backbone.js集合?

我需要在Collection.length-2位置的Collection中插入一个模型.集合中的最后一个模型应该始终保持集合中的最后一个模型.

到目前为止我尝试了什么:

我在Collection"Pages"中添加了一个"页面"模型,然后尝试通过更改它们的顺序来交换它们:

var insertedpage = Pages.at(Pages.length-1);
var lastpage = Pages.at(Pages.length-2);
insertedpage.set({sequence: Pages.length-1});
lastpage.set({sequence: Pages.length});
Run Code Online (Sandbox Code Playgroud)

我还试图删除最后一页,然后添加一个新页面,然后重新添加最后一页.

var lastpage =  Pages.pop();
Pages.add({example1: example2});
Pages.push(lastpage);
Run Code Online (Sandbox Code Playgroud)

这些都不起作用.新添加的页面仍显示为Collection中的最后一个模型.在此之后我需要调用某种订单功能吗?

javascript jquery backbone.js

17
推荐指数
3
解决办法
2万
查看次数

angular.js $ http.post不工作​​ - 没有错误

我正在尝试使用$ http提交新评论.你可能从标题中猜到了它:它不起作用.我尝试了岸版和长版,都失败了.没有控制台错误.

这是我的代码:

$scope.comment = {};
$scope.comment["comment"] = message; // message defined somewhere else

$http.post('/api/items/'+$scope.item.id+'/comments', $scope.comment)
    .success(function(data, status, headers, config) {
         // this isn't happening:
         console.debug("saved comment", $scope.comment);
    })
    .error(function(data, status, headers, config) {
         // this isn't happening:
         console.debug("saved comment", $scope.comment);
    })
}
Run Code Online (Sandbox Code Playgroud)

任何人都知道如何使这项工作?谢谢!

更新:

我现在正在做一个Jquery ajax调用,它运行正常.尽管如此,让它与角度一起工作会很好.这是我的JQuery代码:

            var request = $.ajax({
                url: '/api/items/'+$scope.item.id+'/comments',
                type: "POST",
                data: JSON.stringify($scope.comment),
                contentType: 'application/json; charset=utf-8',
                dataType: "json"
            });

            request.done(function(msg) {
                console.debug("saved comment", $scope.comment);
            });

            request.fail(function(jqXHR, textStatus) {
                alert( "Request failed: " + textStatus );
            }); …
Run Code Online (Sandbox Code Playgroud)

angularjs

17
推荐指数
3
解决办法
4万
查看次数

更新嵌套的$ scope数组时,Angular.js视图不会更新

我正在尝试在添加注释时使angular.js视图更新.我的代码如下:

<div class="comment clearfix" data-ng-repeat="comment in currentItem.comments" data-ng-class="{bubble: $first}" data-ng-instant>
    <div>
        <p>
            <span class="username">{{comment.user}}</span> {{comment.message}}
        </p>
        <p class="time">
            10 minutes ago
        </p>
    </div>
</div>
<div class="comment reply">
    <div class="row-fluid">
        <div class="span1">
            <img src="assets/img/samples/user2.jpg" alt="user2" />
        </div>
        <div class="span11">
            <textarea class="input-block-level addComment" type="text" placeholder="Reply…"></textarea>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

范围在输入时更新:

$('.addComment').keypress(function(e) {
    if(e.which == 10 || e.which == 13) {
        $scope.currentItem.comments.push({
            "user": "user3",
            "message": $(this).val()
        });
        console.debug("currentItem", $scope.currentItem);
    }
});
Run Code Online (Sandbox Code Playgroud)

调试$ scope.currentItem显示已添加注释,但视图不显示新注释.我怀疑$ scope仅在第一级被监视,这就是视图不更新的原因.那是这样吗?如果是这样,我该如何解决?

解决方案:正如Ajay在下面的回答中建议的那样,我将数组推送到apply函数中,如下所示:

var el=$(this);
$scope.$apply(function () {
     $scope.currentChallenge.comments.push({
         "user": $scope.currentUser,
         "message": …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs angularjs-scope angularjs-ng-repeat

12
推荐指数
1
解决办法
2万
查看次数

angular.js广播错误:$ rootScope.broadcast不是一个函数

我正在动态地将项目添加到项目数组中,并希望让指令知道有新项目.所以在控制器函数addItem()内部我尝试执行以下操作:

setTimeout(function(){
    $rootScope.broadcast("itemAdded");
    // I also tried: $scope.broadcast("itemAdded"); which isn't working
});
Run Code Online (Sandbox Code Playgroud)

问题:我收到错误:

$ rootScope.broadcast不是一个函数

angularjs angularjs-directive angularjs-scope

6
推荐指数
1
解决办法
5585
查看次数

为什么光滑的轮播不使用browserify?

我正在尝试使用光滑的旋转木马(http://kenwheeler.github.io/slick/)并通过npm安装.

通过browserify包含它,如下所示:

slick = require('slick-carousel')
Run Code Online (Sandbox Code Playgroud)

试图像这样运行:

$('.gallery__carousel').slick();
Run Code Online (Sandbox Code Playgroud)

没有控制台错误,轮播没有初始化.这是怎么回事?

jquery browserify slick.js

6
推荐指数
1
解决办法
5008
查看次数

如何根据参数创建使用$ resource返回数据的服务

我想创建一个服务,根据参数调用后端.这段代码不起作用,但我希望它能说明我想要实现的目标:

myproject.factory('Item', function($resource) {
    if (@id !== undefined)
        return $resource('/resource/item/:id/item.json', {id: '@id'});
    else if (@userid !== undefined)
        return $resource('/resource/user/:userid/items.json', {userid: '@userid'});
});
Run Code Online (Sandbox Code Playgroud)

基本上第一个选项只返回一个项目的id,第二个选项返回userid的几个项目.

我希望能够像这样调用该服务:

$scope.item = Item.get({id: id}, function() {...}
Run Code Online (Sandbox Code Playgroud)

要么

$scope.items = Item.query({userid: userid}, function() {...}
Run Code Online (Sandbox Code Playgroud)

实现类似目标的最佳方式是什么?我是否需要为所有内容创建单独的服务?

谢谢!

javascript angularjs

3
推荐指数
1
解决办法
1011
查看次数

如何将数组传递给指令而不将其转换为字符串?

是的,所以我只是进入指令,他们似乎非常棒.我遇到了一个问题:

我需要将一个图像数组传递给一个指令,以便我可以按照某些标准过滤它们.这是我的html调用指令:

<img cover="{{challenge.images}}">
Run Code Online (Sandbox Code Playgroud)

这是我的指示:

myproject.directive('cover', function() {
    return {
        link: function ($scope, element, attrs) {
            console.debug("attrs.cover", Array(attrs.cover));
        }
    };
});
Run Code Online (Sandbox Code Playgroud)

输出是一个字符串.有没有办法防止attr变成String?

angularjs angularjs-directive

3
推荐指数
1
解决办法
2126
查看次数

在angular.js应用程序中全局执行错误处理的最佳方法是什么?

我有一个应用程序,它使用我创建的各种服务进行http调用.当服务器请求失败或api返回错误属性时,我想在页面顶部显示这些错误.

我可以在角度文档中看到错误最好以这种方式显示,这是有道理的:

<div ng-controller="AlertDemoCtrl">
  <alert ng-repeat="alert in alerts" type="alert.type" close="closeAlert($index)">{{alert.msg}}</alert>
</div>
Run Code Online (Sandbox Code Playgroud)

并在AlertDemoCtrl中:

  $scope.addAlert = function() {
    $scope.alerts.push({msg: "Another alert!"});
  };

  $scope.closeAlert = function(index) {
    $scope.alerts.splice(index, 1);
  };
Run Code Online (Sandbox Code Playgroud)

问题是我不确定如何将错误推送到AlertDemoCtrl中的警报,因为http请求发生在我无法访问AlertDemoCtrl的服务中.我是否需要广播事件才能将它们添加到AlertDemoCtrl中,或者是否有办法将控制器注入服务?或者我需要将addAlert函数添加到$ rootScope中吗?

感谢任何建议,谢谢!

angularjs

3
推荐指数
1
解决办法
2604
查看次数