[英]Pass parameters from html directive to controller
I would like to pass an argument from an html directive to a controller Example: 我想将参数从html指令传递给控制器示例:
Directive: 指示:
angular.module('app')
.directive('helloWorld', function () {
return {
replace: false,
restrict: 'AE',
templateUrl: "./views/templates/helloWorld.html"
}
});
helloWorld.html: helloWorld.html:
<body ng-app="app" >
<div ng-controller="HelloWorldCtrl">
{{ welcome }}
</div>
hello.html: hello.html:
<body ng-app="app">
<hello-world/>
</body>
HelloWorldCtrl: HelloWorldCtrl:
angular.module('app')
.controller('HomeWorldCtrl', function ($scope, ) {
$scope.welcome = "Welcome"
};
})
Can I specify a parameter in hello.html eg 我可以在hello.html中指定参数吗?
<hello-world param="param1"/>
That is being passed into the controller? 那是传递给控制器吗? So in the HomeWorldCtrl I can check the value of the parameter?
因此,在HomeWorldCtrl中,我可以检查参数的值吗? Are there any better alternatives to achieve this?
有没有更好的选择来实现这一目标?
Thanks, 谢谢,
app.directive('helloWorld', function(){
return {
restrict:'E',
scope: {
param: '@'
},
template:'<div class="param"><h2>{{param}}</h3></div>'
};
});
// you have options to choose from
//= is two-way binding
//@ simply reads the value (one-way binding)
//& is used to bind functions
<hello-world="someParam"></hello-world>
// for the scope definition:
scope: {
title: '@helloWorld'
}
The usage in the template will remain the same
<hello-world param="someParam"></hello-world>
If you isn't use isolated scope (scope: {someparam: ""}), then you may use any $scope properties in the directive template without changing anything: 如果您不使用隔离范围(范围:{someparam:“”}),则可以在指令模板中使用任何$ scope属性,而无需进行任何更改:
$scope.param = "new param value";
..
return {
..
,template: "<param>{{param}}</param>"
Thanks! 谢谢!
directive 指示
return {
replace: false,
restrict: 'AE',
templateUrl: "./views/templates/PatientSearchEdit.html",
scope: {
param: '@'
}
}
controller 控制者
console.log($scope.param);
Logs indeed the value specified. 确实记录指定的值。 Thanks you very much!
非常感谢你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.