Angularjs 从父控制器访问数据,控制器为
我正在阅读布拉德·戴利的《学习角Js》一书。本书在示例中使用了Angularjs 从父控制器访问数据,控制器为,angularjs,angular-directive,Angularjs,Angular Directive,我正在阅读布拉德·戴利的《学习角Js》一书。本书在示例中使用了$scope。我强迫自己使用controllerAs。在第七章中,本书重点介绍如何创建自定义指令 我创建了一个与提供的示例类似的简单示例。在我的内心,我正在将transclude变为现实。我正在使用link函数向父div追加一个页脚。在页脚标记的内部,书的作者调用scope.$parent.title在示例中,title值来自父控制器 .directive('myBox', function() { return
$scope
。我强迫自己使用controllerAs
。在第七章中,本书重点介绍如何创建自定义指令
我创建了一个与提供的示例类似的简单示例。在我的内心,我正在将transclude变为现实。我正在使用link函数向父div追加一个页脚。在页脚标记的内部,书的作者调用scope.$parent.title
在示例中,title
值来自父控制器
.directive('myBox', function() {
return {
transclude: true,
restrict: 'E',
scope: {title: '@', bwidth: '@bwidth'},
template: "<div><span class='titleBar'>{{title}}"+ "</span> <div ng-transclude></div></div>",
link:function(scope, elem, attr, controller, transclude) {
console.log('scope', scope.$parent)
console.log('controller', controller);
elem.append('<span class="footer">'+ scope.$parent.title + '</span>');
elem.css('border', '2px ridge black');
elem.css('display', 'block');
elem.css('width', scope.bwidth);
}
}
})
这是我正在尝试的东西
- 当您使用ng controller语法时,它只是在作用域中调用控制器的成员,如“start()”
注意
您在控制台中输出的控制器是属于您尚未定义的指令的控制器。您能提供一个完整的示例来说明您的问题吗?下面是一个基本的例子
function FunCtrl() {
var vm = this;
vm.title = "myApplication";
}
<input ng-click="start()" type="button" value="Button"/>
<input ng-click="fun.start()" type="button" value="Button"/>
scope.$parent.fun.title