将参数从 html 指令传递到控制器
Pass parameters from html directive to controller
我想将参数从 html 指令传递给控制器例:
命令:
angular.module('app')
.directive('helloWorld', function () {
return {
replace: false,
restrict: 'AE',
templateUrl: "./views/templates/helloWorld.html"
}
});
你好世界.html:
<body ng-app="app" >
<div ng-controller="HelloWorldCtrl">
{{ welcome }}
</div>
您好.html:
<body ng-app="app">
<hello-world/>
</body>
HelloWorldCtrl:
angular.module('app')
.controller('HomeWorldCtrl', function ($scope, ) {
$scope.welcome = "Welcome"
};
})
我可以在 hello 中指定一个参数吗.html例如
<hello-world param="param1"/>
这正在传递到控制器中?所以在 HomeWorldCtrl 中我可以检查参数的值吗?有没有更好的选择来实现这一点?
谢谢
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>
如果您不使用隔离作用域(作用域:{someparam:"}),则可以使用指令模板中的任何$scope属性,而无需更改任何内容:
$scope.param = "new param value";
..
return {
..
,template: "<param>{{param}}</param>"
谢谢!
命令
return {
replace: false,
restrict: 'AE',
templateUrl: "./views/templates/PatientSearchEdit.html",
scope: {
param: '@'
}
}
控制器
console.log($scope.param);
确实记录指定的值。非常感谢!
相关文章:
- 在指令控制器中使用$attrs时出现问题
- 从控制器继承了隔离的作用域以生成可重用的指令
- 指令中的控制器问题
- 如何将角度输入指令数据传递给控制器
- 当使用控制器作为语法时,如何从父指令继承属性
- 将指令绑定到控制器属性
- Angularjs在获得指令中的输出后,将值分配给控制器
- 将方法从控制器注入到未使用右变量调用的指令
- Angular,从指令控制器中的控制器触发函数
- 如何在指令链接函数中使用从控制器传递的筛选器
- 角度控制器功能赢得'不按指令工作
- 角度指令控制器:参数不是函数,未定义
- 通过指令在控制器中执行javascript函数
- AngularJS执行指令模板中的控制器函数
- AngularJS中的控制器/指令继承
- 在同一元素上使用“控制器作为 x”的多个 ng 控制器指令
- AngularJS ng控制器指令不接受javascript中的变量(作用域函数),也不给出任何错误
- 如何在AngularJS中测试控制器指令
- 控制器+指令中的角度范围
- AngularJS:使用在控制器/指令中别处定义的自定义javascript函数