6

鉴于这个简单的 Angular 模块:

angular.module('fixturesModule', [])
.directive('clubfixtures', function () {
    "use strict";
    return {
        restrict: 'E',
        replace: true,
        transclude: true,
        scope: {
            club : "@club",
            max : "@max"
        },

    templateUrl: "ClubResultsTemplate.html",

    controller: function ($scope, $http) {

        $http.get("data.json").success(function (data) {
            $scope.results = data;
        });

        $scope.sortBy = "Date";

    }
}
});

如何在控制器功能中访问俱乐部和最大值?

谢谢,杰夫

4

2 回答 2

10

范围内的属性使用“@”设置,如在调用控制器函数scope: { myAttr: '@' }接收它们的值。

您可以使用简单的 setTimeout 来演示这一点 - 请参阅http://jsfiddle.net/Q4seC/(请务必打开控制台)

如您所见,$attrs 已在您需要时准备就绪。

有趣的是,如果您使用 '=' 而不是 '@',则该值已准备好并且可用,这让我认为这可能被视为 Angular 中的错误......

于 2013-02-20T12:41:47.727 回答
5

提到的 2 个变量 (maxclub) 将简单地定义在注入指令控制器的范围内。这意味着您可以编写:

controller: function ($scope, $http) {

        $scope.max; //do sth with $scope.max
        $scope.club //so sth with $scope.club

        $http.get("data.json").success(function (data) {
            $scope.results = data;
        });
}

在您的指令的控制器中。

如果您想了解更多信息,我建议在http://docs.angularjs.org/guide/directive中使用“指令定义对象”,其中讨论指令中的范围。

于 2012-11-01T17:32:41.813 回答