我有这个:
app.controller('foo1', function ($scope) {
$scope.bar = 'foo';
});
app.controller('foo2', function ($scope) {
// want to access the $scope of foo1 here, to access bar
});
Run Code Online (Sandbox Code Playgroud)
我怎么做到这一点?
我正在写一个angularJS应用程序.在这个特定的控制器中,我通过该$window.open服务打开一个新的浏览器窗口.但是在新窗口中,所有$scope变量都会丢失.
我尝试使用,window.parent但这不起作用.事实上,在新的浏览器窗口中,所有应用程序服务,控制器或作用域根本不起作用,这是真的吗?有没有办法打开一个新的浏览器窗口,但仍然使新窗口属于旧窗口中相同的角度应用程序?我需要访问一些angularJS服务和打开该新窗口的控制器的范围.这有可能吗?
javascript angularjs angularjs-service angularjs-scope angularjs-controller
我已经定义了一个控制器,并将其应用于两个视图,差别很小.
角度代码:
app.controller('MyCtrl', function($scope) {
$scope.canSave = false;
$scope.demo = {
files : [{
filename: 'aaa.html',
source: '<div>aaa</div>'
}, {
filename: 'bbb.html',
source: '<div>bbb</div>'
}]
}
$scope.newFile = function(file) {
$scope.demo.files.push(file);
}
$scope.$watch("demo.files", function(val) {
$scope.canSave = true;
}, true);
});
Run Code Online (Sandbox Code Playgroud)
查看1:
<div ng-controller="MyCtrl"></div>
Run Code Online (Sandbox Code Playgroud)
观点2:
<div ng-controller="MyCtrl"></div>
Run Code Online (Sandbox Code Playgroud)
示例代码非常简单,但我的实际项目中有很多代码和逻辑.
视图1和2具有几乎相同的功能,只有一些差异,但我需要在控制器中为每个功能编写一些代码.
我不想为它们创建2个不同的控制器,因为它们具有大部分相同的逻辑.我不想将逻辑移动到服务以在2个控制器之间共享它,因为逻辑并不是服务的常见逻辑.
还有其他办法吗?
我正在使用带有别名控制器模式的AngularJS.我无法从父控制器访问(或者我不知道如何)指令方法.
我的控制器中有一个函数应该调用一个指令方法,但这个指令方法在this控制器值内不可用.
这就是我所拥有的.我做错了什么?
JS
angular.module('myApp', []).
controller('MyCtrl', function(){
this.text = 'Controller text';
this.dirText = 'Directive text';
this.click = function(){
this.changeText();
}
})
.directive('myDir', function(){
return {
restrict: 'E',
scope: {
text: '='
},
link: function(scope, element, attrs){
scope.changeText = function(){
scope.text = 'New directive text';
};
},
template: '<h2>{{text}}</h2>'
};
});
Run Code Online (Sandbox Code Playgroud)
HTML
<div ng-app="myApp">
<div ng-controller="MyCtrl as ctrl">
<h1>{{ctrl.text}}</h1>
<my-dir text="ctrl.dirText"></my-dir>
<button ng-click="ctrl.click()">Change Directive Text</button>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这里有代码的codepen.
javascript events angularjs angularjs-directive angularjs-controller
当我尝试对我的控制器进行单元测试时,我收到了错误.当我调试测试用例时,我希望得到预期的值,但是它失败了以下错误.我在这里丢失的是什么,或者我是否在错误中测试控制器变量方式?
我的spec文件如下
'use strict';
describe('Information.controller', function () {
beforeEach(angular.mock.module('asp'));
var InformationController, scope;
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
InformationController = $controller('InformationController', {
$scope: scope
});
}));
fit('Should remove the object without info from the object', function () {
InformationController.Data = [
{
"ban": "03745645455",
"accountNo": "0000drgyt456456565",
"id": "2c4cc5e8-f360367"
},
{
"ban": "27346fgh9080",
"accountNo": "000456456ytr655680",
"id": "2c4cc8ae-50bbf360367"
}
];
InformationController.banList = InformationController.Data.map((value, index) => (value && value.ban ? {'id': index, 'text': value.ban} : null))
.filter(value => value);
expect(InformationController.banList.length).toBe(3);
});
});
Run Code Online (Sandbox Code Playgroud) 这是一个现实生活中的角度问题,我无法解决这个问题.我喜欢Angular,但这个问题现在让我烦恼不已.
扩展现有控制器功能的最佳实践是什么,并在我的应用程序的其他页面上使用扩展控制器?换句话说:如何在Angular中进行控制器继承?
编辑出来 - 2014年9月23日,不要认为我原来的用例的描述可以帮助访问者更好地理解我在这里的意思.我认为它将人们从真正的问题中解脱出来.
我遇到了我认为是常见的场景,我在其中使用MVC模式(特别是ASP.NET的MVC框架)用于Web应用程序,前端使用AngularJS.我的问题是我有一个特定的值,它是模型的一部分传递给我的视图,我也想让我的Angular控制器的$ scope,理想情况下,一旦控制器被初始化.
如何做到这一点是以前被问及回答的问题.有一个显而易见的候选人:ngInit.然而,在某些时候,Angular更新了他们的文档,似乎是针对这一具体想法的警告:
唯一合适的用途
ngInit是用于别名的特殊属性ngRepeat,如下面的演示中所示.除了这种情况,您应该使用控制器而不是ngInit初始化范围上的值.
建议的替代方案并不十分相关.
当然,还有其他我能想到的解决方法.例如,视图可以将值插入到ngModel隐藏输入的指令中.或者我可以简单地忽略警告并使用ngInit无论如何.但是,我能想到的任何一种方式都是一种更丑陋的方式来做同样的事情ngInit,或者显然更糟.
事实上,对我来说似乎是明显的解决方案显然是错误的,这可能是我的心态与Angular应该如何完成无关的指标.所以我的问题不是"我该如何处理这种情况",而是:
ngInit?澄清,因为从前两个评论来看,这一点并不清楚:这是针对某个或大部分页面直接作为MVC视图提供的情况,只有一些特定的功能由Angular提供.我想传递给Angular控制器的数据已经传递给模型中的视图.我不希望Angular控制器然后去为服务器做自己的get请求,只是为了获得已经以不同格式提供给视图的相同参数.
javascript asp.net-mvc angularjs angularjs-controller ng-init
我刚刚开始从w3schools学习AngularJS .我正在尝试练习他们在教程中提到过的示例.一切正常,但是当我来到"AngularJS控制器"时,它在w3schools中运行良好并不能正常工作自己动手».我把我的代码分成了这个小提琴的例子.我的脚本看起来像这样:
function personController($scope) {
$scope.firstName= "John";
$scope.lastName= "Doe";
}
Run Code Online (Sandbox Code Playgroud)
尽量帮助我,并建议我一个很好的教程(或任何免费的PDF文件).
我试图以递归方式到达父"box"指令的控制器:
<body ng-app="main">
<!-- no nesting: parent is the just body -->
<box></box>
<script type="text/javascript">
angular.module('main', [])
.directive('box', function() {
return {
restrict: 'E',
controller: function() { },
require: '?^box', // find optional PARENT "box" directive
link: function(scope, iElement, iAttrs, controller) {
// controller should be undefined, as there is no parent box
alert('Controller found: ' + (controller !== undefined));
}
};
});
</script>
</body>
Run Code Online (Sandbox Code Playgroud)
我希望控制器变量undefined在链接函数中,但我得到实际box指令的控制器.
所以我的问题是......在这种情况下如何访问PARENT控制器:
<box>
<box></box>
</box>
Run Code Online (Sandbox Code Playgroud)
plunk:http://plnkr.co/edit/85Wl5W如果我在同一个控制器(modalController.js)上使用$ modalInstance,而不是处于模态,则angular会被冻结.
我只想用angular-ui bootstrap模态服务使用angularjs来简化我的生活.我想在一个单独的文件上使用相同的控制器,但也在一个模态上.也就是说,我希望他们做同样的任务.有没有正确的方法呢?
例如modal.html,modalController.js.我想在模态窗口上显示这些,但在我的应用程序中也没有模态.问题是,如果我使用相同的控制器,我不能注入$ modalInstance,因为如果没有模态,它是未定义的.
提前致谢,
亚历克斯
modal-dialog angularjs angular-ui angular-ui-bootstrap angularjs-controller
angularjs ×10
javascript ×6
angular-ui ×1
asp.net-mvc ×1
events ×1
html ×1
modal-dialog ×1
ng-init ×1
unit-testing ×1