通过ng-include中的params

时间:2015-07-23 07:04:32

标签: javascript html angularjs templates

我是angularjs的新手。我有一个要求,我在每个部分HTML中一次又一次地使用相同的单选按钮。所以,我想把它作为一个通用模板,并在其他部分模板中使用它。

<ng-include src="'partial-htmls/toolbox/radio-button.html'"></ng-include>

其中显示单选按钮如下:

Sex :    O Male
         O Female

这很好,但我需要在ng-include中以某种方式更改单选按钮的标签,文本和数量。 (我想通过以某种方式传递params来做

我来自lodash的背景,可以轻松处理。我认为也可能有角度的方式。

请帮忙。

1 个答案:

答案 0 :(得分:7)

无法将参数传递给ng-include 可以访问与其所在HTML相同的范围。

如果您需要能够传递不同的参数,则必须使用指令:

angular.module("myModule")
.directive('radioButton', [function () {
    return {
        restrict: 'E',
        scope: {
            pass: "=",
            some: "@",
            properties: "="
        },
        templateUrl: 'app/directives/views/radio-row.html',
        controller: ["$scope", function ($scope) {
            // Isolated $scope here
        }]
    };
}]);
<radio-button pass="parentScopeObject" some="Literal string" properties="parentScopeSomething"></radio-button>