ng-repeat不会刷新WITH $ apply

时间:2014-03-27 12:20:25

标签: angularjs angularjs-directive angularjs-scope angularjs-ng-repeat

这是我正在使用的代码(并且已经在整个项目中使用),范围已更新,但ng-repeat不会刷新,我正在使用范围。$ apply ..不知道为什么,少数开发人员还看了一下代码..没有解决方案..

指令:

app.directive("addBrandSettings", function(){
    return {
        restrict: "A",
        link: function(scope, element, attrs){
            element.bind("keypress", function(e){
                if(e.which === 13){
                    var brand = element.val();
                    scope.$apply(function(){
                        scope.settings.brands.push(brand);
                        console.log(scope.settings.brands);
                    })
                    element.val("");
                }
            })
        }
    }
});

HTML:

<input add-brand-settings type="text" placeholder="Add Brand"/>
<p ng-repeat="brand in settings.brands">{{brand}}<a remove-brand-settings index="{{$index}}" href="#"><i class="fa fa-times-circle"></i></a></p>

范围:

    $scope.settings = {
        companyInfo: {
            name: "",
            email: "",
            phone: "",
            website: ""
        },
        users: [
            {
                username: "Supreme Manager",
                role: "Super User",
                password: "asdasd"
            },
            {
                username: "Regular Grunt",
                role: "User",
                password: "asdasd"
            }
        ],
        brands: [
            "Maxi",
            "Chipsy",
            "Bananice"
        ],
        retailers: [
            "Maxi",
            "Ikea",
            "Tempo"
        ]
    }

1 个答案:

答案 0 :(得分:1)

您的代码完美无缺,因此您可能会遇到一些语法问题,这是一个有效的例子:

var app=angular.module('App', []);
function ctrl($scope){
   $scope.settings = {
        companyInfo: {
            name: "",
            email: "",
            phone: "",
            website: ""
        },
        users: [
            {
                username: "Supreme Manager",
                role: "Super User",
                password: "asdasd"
            },
            {
                username: "Regular Grunt",
                role: "User",
                password: "asdasd"
            }
        ],
        brands: [
            "Maxi",
            "Chipsy",
            "Bananice"
        ],
        retailers: [
            "Maxi",
            "Ikea",
            "Tempo"
        ]
    }
}
app.directive("addBrandSettings", function(){
    return {
        restrict: "A",
        link: function(scope, element, attrs){
            element.bind("keypress", function(e){
                if(e.which === 13){
                    var brand = element.val();
                    scope.$apply(function(){
                        scope.settings.brands.push(brand);
                        console.log(scope.settings.brands);
                    })
                    element.val("");
                }
            })
        }
    }
});

HTML:

<div ng-app="App"  ng-controller="ctrl">
<input add-brand-settings type="text" placeholder="Add Brand"/>
<p ng-repeat="brand in settings.brands">{{brand}}<a remove-brand-settings index="{{$index}}" href="#"><i class="fa fa-times-circle"></i></a></p>
</div>

实例:http://jsfiddle.net/choroshin/7zVd2/