Javascript ng中的传递参数包括
我对angularjs是新手。我有一个要求,我在每个部分html中反复使用相同的单选按钮。因此,我想将其作为一个通用模板,并在其他部分模板中使用它Javascript ng中的传递参数包括,javascript,html,angularjs,templates,Javascript,Html,Angularjs,Templates,我对angularjs是新手。我有一个要求,我在每个部分html中反复使用相同的单选按钮。因此,我想将其作为一个通用模板,并在其他部分模板中使用它 <ng-include src="'partial-htmls/toolbox/radio-button.html'"></ng-include> 这很好,但我需要以某种方式更改ng include中单选按钮的标签、文本和数量。(我想通过传递params来实现) 我来自lodash的背景,在那里它很容易处理。我想也许也有办法
<ng-include src="'partial-htmls/toolbox/radio-button.html'"></ng-include>
这很好,但我需要以某种方式更改ng include
中单选按钮的标签、文本和数量。(我想通过传递params来实现)
我来自lodash的背景,在那里它很容易处理。我想也许也有办法
请帮助。无法将参数传递给
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
}]
};
}]);
无法将参数传递给
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
}]
};
}]);
这似乎是ng include的一个奇怪用法。为什么不使用嵌套控制器?@Amit我想我应该使用ng include
请告诉我处理这个问题的最佳方法。这看起来像是ng include的一个奇怪用法。为什么不使用嵌套控制器?@Amit我想我应该使用ng include
请建议我处理此问题的最佳方法。您可以将ng控制器
或ng init
放在与ng include
相同的元素上,但是一个指令会更好。当我把自定义属性用单引号括起来时,它就起作用了。如下所示:pass=“'parentScopeObject'”some=“'Literal string'”properties=“'parentScopeSomething'”
。谢谢@格林先生:对于需要使用“@”的字符串,则不需要额外的引号。基本上,“@”
将直接获取传递参数的内容,“=”
将参数解释为父范围中的变量名。您可以将ng控制器
或ng init
放在与ng include
相同的元素上,但使用指令会更好。当我将自定义属性用单引号括起来时,这就起到了作用。如下所示:pass=“'parentScopeObject'”some=“'Literal string'”properties=“'parentScopeSomething'”
。谢谢@格林先生:对于需要使用“@”的字符串,则不需要额外的引号。基本上,“@”
将直接获取传递参数的内容,“=”
将参数解释为父范围中的变量名。
<radio-button pass="parentScopeObject" some="Literal string" properties="parentScopeSomething"></radio-button>