$scope不会使用点表示法从控制器注入/继承到指令
$scope not being injected/inherited from controller to directive with dot notation
我在将范围从控制器注入/继承到指令时遇到问题。重要的是,模块没有被分离成自己的变量。
angular.module('articles').controller('ArticlesController', ['$scope, ...
]).directive('comments', ['$scope' ... //does not work!
您不会将作用域作为依赖项注入指令中。应该是这样的:
.directive([function() {
return {
"link": function($scope, $element, $attrs) {
//code here
}
}
}]);
最好的方法是将指令视为黑匣子。您向它提供一些数据,它会更新/显示它。您可以在此处阅读有关指令的所有必需信息,并声明指令的输入和输出,如下所示:
.directive('myDirective', function() {
return {
restrict: 'E',
scope: {
myParameter: '=' //this declares directive's interface (2 way binded)
},
link: function (scope, element) {
console.log(scope.myParameter);
}
};
});
然后,您可以将此指令用作:
<my-directive my-parameter='variable_declared_in_controller'></my-directive>
我只是用其他几个作用域完全绕过了$scope
:
.directive('comments', ['$stateParams', '$location', 'Articles',
function ( $stateParams, $location, Articles) {
if ($stateParams.articleId != null){
var article = Articles.get({
articleId: $stateParams.articleId
});
var comments = {articleId: article.articleId, created: Date.now, comment:"Hello, World!", user: "admin" };
return{
restrict: "A",
scope: true,
template: "<div></div>"
};
}
}
]);
相关文章:
- 无法在routeprovider中注入控制器
- 如何检查注入控制器中的功能
- 将模块注入控制器AngularJs
- 我可以在不注入控制器的情况下在 Angular 中使用全局常量吗?
- 角度 js 自定义服务/工厂无法注入控制器/配置
- 路由器解析不会注入控制器
- 工厂没有被注入控制器 - angularJS
- 使用 Karma 进行角度测试:注入控制器后,$controller() 未定义
- 角度:需要从模块注入控制器
- 服务无法注入控制器
- UI路由器可以't将解析的对象注入控制器
- 从Ember Addon中注入控制器操作
- AngularJS,将服务注入控制器
- (angularjs)得到'ReferenceError:$scope未定义'当注入控制器时
- 将服务动态注入控制器
- 不能直接注入控制器
- 如何在AngularJS中注入控制器
- 将工厂注入控制器angularjs + typescript
- 如何将变量注入控制器
- 如何将工厂依赖性注入控制器的角度