如何$scope使用.$emit和.$on方法将对象从一个控制器发送到另一个控制器?
function firstCtrl($scope) {
$scope.$emit('someEvent', [1,2,3]);
}
function secondCtrl($scope) {
$scope.$on('someEvent', function(mass) { console.log(mass); });
}
Run Code Online (Sandbox Code Playgroud)
它不像我认为的那样工作.怎么做$emit和$on工作?
我需要在角度j的另一个控制器中调用函数.如果有可能,请提前帮助我
代码:
app.controller('One', ['$scope',
function($scope) {
$scope.parentmethod = function() {
// task
}
}
]);
app.controller('two', ['$scope',
function($scope) {
$scope.childmethod = function() {
// Here i want to call parentmethod of One controller
}
}
]);
Run Code Online (Sandbox Code Playgroud) 是否可以将控制器注入另一个属于同一模块的控制器?
例:
var app = angular.module('myAppModule', [])
.controller('controllerOne', ['$scope', function($scope){
$scope.helloWorld = function(){
return 'Hello World';
}
}])
.controller('controllerTwo', ['$scope', 'controllerOne', function($scope, controllerOne){
console.log(controllerOne.helloWorld());
}])Run Code Online (Sandbox Code Playgroud)
我一直在controllerOne上得到未知的提供者.我不知道它是如何可能的,因为它们在同一个模块中共存.任何帮助将非常感激.
javascript dependency-injection angularjs angularjs-scope angularjs-controller
我想在另一个控制器中共享一个控制器的$ scope函数,在本例中是一个AngularUI对话框.
特别是在下面的例子中,我希望$ scope.scopeVar在PopupCtrl中可用.
main.js
angular.module('MyApp', ['ui.bootstrap']);
var MainCtrl = ['$scope', '$dialog', '$rootScope', function($scope, $dialog, $rootScope) {
$scope.myTestVar = "hello";
$scope.myOpts = {
backdrop: true,
keyboard: true,
backdropClick: true,
resolve: { MainCtrl: function() { return MainCtrl; }},
templateUrl: 'myPopup.html',
controller: 'PopupCtrl'
};
$scope.scopeVar = 'scope var string that should appear in both index.html and myPopup.html.';
$rootScope.rootScopeVar = "rootScope var string that should appear in both index.html and myPopup.html.";
$scope.openDialog = function() {
var d = $dialog.dialog($scope.myOpts);
d.open().then(function() { …Run Code Online (Sandbox Code Playgroud) 我试图获得一个名为"容器"的div id,如下所示:
var chart = $(#container).highcharts();
当我通过我的html页面中定义的控制器调用时,它完美地工作.但是当我试图通过我的Html页面没有引用的另一个控制器调用此函数时,它无法正常工作.
那么,如何在其他html页面中关联的其他控制器中调用我的div?
例:
它工作并在我的控制器"main"中定义,它在我的html页面"main.html"中定义
Item.Rest("datePerUser",null).then(function(totalDatePerUser){
var objectDate = totalDatePerUser.data;
var femDate = objectDate.Chart1Fem;
var mascDate = objectDate.Chart1Masc;
loadDataChart1(mascDate, femDate);
});
Run Code Online (Sandbox Code Playgroud)
它不起作用,并在我的控制器"菜单"中定义,该菜单在我的html页面"menu.html"中定义
PeriodItem.Rest("datePerUser_",Obj).then(function(datePerUserTeste){
var objectDate = datePerUserTeste.data;
newFemDate = objectDate.Chart1Fem;
newMaleDate = objectDate.Chart1Masc;
loadDataChart1(newMaleDate, newFemDate);
});
Run Code Online (Sandbox Code Playgroud)
两者都调用了跟随功能
function loadDataChart1(dataMale, dataFem){
var chartProfiles = $('#container').highcharts();
obj_female = JSON.parse('{ "data":' + dataFem + ' }');
obj_male = JSON.parse('{ "data":' + dataMale + ' }');
chartProfiles.series[0].update(obj_female);
chartProfiles.series[1].update(obj_male);
}
Run Code Online (Sandbox Code Playgroud)
仅仅是为了获取信息,我正在使用angularJS和Rest服务,从数据库获取数据,我有多个Html页面.
如何将我选中的项目存储在带有其他控制器的复选框中?
我的尝试(请参阅plnkr查看视图):
var myApp = angular.module('myApp', []);
myApp.factory('CooSelection', function () {
return {selectedCoo: []}
})
function CooListCtrl($scope, CooSelection) {
$scope.coos = {"Coos": ["spark", "nark", "hark", "quark"]};
$scope.coo_list_selection = CooSelection;
$scope.checkSelection = function (item) {
if ($scope.coo_list_selection.indexOf(item) === -1) {
$scope.coo_list_selection.push(item);
} else {
$scope.coo_list_selection.splice($scope.coo_list_selection.lastIndexOf(item), 1);
}
}
}
CooListCtrl.$inject = ['$scope', 'CooSelection'];
function DebugCooList($scope, CooSelection) {
$scope.coo_selection = CooSelection;
}
DebugCooList.$inject = ['$scope', 'CooSelection'];
Run Code Online (Sandbox Code Playgroud) javascript angularjs angularjs-directive angularjs-scope angularjs-controller
考虑两个带有隔离范围的嵌套指令:
<dctv1>
<dctv2></dctv2>
<dctv1>
Run Code Online (Sandbox Code Playgroud)
如果我想dctv2和dctv1我交谈,我可以选择:
dctv1在定义中dctv2使用require:'^dctv1'<dctv2 callParent="hello()"></dctv2>和scope:{callParent:'&'}$scope.$emit在dctv2后来所有父范围会听到该消息.现在我想dctv1谈谈dctv2.
$scope.$broadcast,但所有孩子都会听到.通过这里谈话,我的意思是称为函数或类似函数.不想设置堵塞digestloop的手表.
如何以最佳方式进行dctv1通知dctv2,使它们松散耦合?我应该能够删除dctv2而不会出错.
我有这个代码:
JS:
angular.module("module")
.controller("fooController", ["$scope", function($scope) {
...
})
.directive("foo", function() {
return {
restrict: "E",
controller: "fooController",
link: function($scope, $element, $attrs) {
// Do some things with the scope of the controller here
}
}
})
.directive("bar", function() {
return {
restrict: "E",
require: "fooController",
link: function($scope, $element, $attrs) {
// Nothing yet
}
}
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<html>
<head>
<!-- Scripts here -->
</head>
<body ng-app="module">
<foo/>
<bar/>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
指令foo有效,但指令bar抛出错误:No controller: fooController. …
我试图根据当前视图更改页眉.标头位于ngView之外.这是可能的还是我需要将标题放在视图中?
我的代码看起来类似于:
<div id="header">
<div ng-switch on="pagename">
<div ng-switch-when="home">Welcome!</div>
<div ng-switch-when="product-list">Our products</div>
<div ng-switch-when="contact">Contact us</div>
</div>
(a lot of unrelated code goes here)
</div>
<div id="content>
<div ng-view></div>
</div>
Run Code Online (Sandbox Code Playgroud) 我想编写一些搜索输入来使用ngResource从数据库中获取数据.
数据显示在带有ng-repeat的页面中,但是当我执行搜索并且$ scope已更新时,视图不会更新并显示旧数据.
这是代码:
main.html(活动视图)
<div ng-controller="searchCtrl as searchCtrl" layout="column">
<form class="form-inline search-form col-md-offset-1">
<div class="form-group col-md-5">
<label class="sr-only" for="search_location">Location</label> <input
id="search_location" type="search" class="form-control"
ng-Autocomplete ng-model="place" placeholder="Location" />
</div>
<div class="form-group col-md-5">
<label class="sr-only" for="search_tags">Tags</label> <input
style="width: 100%;" id="search_tags" type="search"
class="form-control" id="search_tags" placeholder="Tags">
</div>
<div class="col-md-1">
<md-button class="md-fab md-mini" aria-label="Search" ng-click="searchCtrl.search()"> <md-icon class="md-fab-center"
md-font-icon="glyphicon glyphicon-search" style="color: black;"></md-icon>
</md-button>
</div>
</form>
</div>
<div ng-controller="mainCtrl">
<div ng-repeat="evento in eventi" ng-include="'views/components/event_card.html'" class="col-md-3"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
main.js
'use strict';
app.factory('Eventi', function($resource) {
return $resource('/eventsws/events/:location', {location : …Run Code Online (Sandbox Code Playgroud) 我是AngularJS的新人.
我试图从另一个控制器调用函数.
我发现有很多方法可以做到这一点.
1)共享服务
2)使用事件
这是我已经检查过的链接.
我认为使用事件是更好的方法.除此之外,我认为当我们使用$ rootscope时,它不会破坏事件参考权吗?
var myModule = angular.module('myapp', []);
myModule.controller('otherController', function($scope) {
$scope.$on('ShowDialog', function(event) {
alert("hello");
});
});
myModule.controller('myController', function($scope) {
$scope.OnClick = function() {
alert("Done");
$scope.$emit('ShowDialog');
}
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div style="padding: 0 5px; height: 100%" ng-app="myapp">
<div style="padding: 0 5px; height: 100%" ng-controller="myController">
<input type="button" ng-click="OnClick()" value="Say Hello!" />
</div>
</div>Run Code Online (Sandbox Code Playgroud)
请让我知道我错过了什么?什么是最好的方法呢?
我有一个嵌套在另一个控制器中的控制器。
<div ng-controller="manufacturerController" ng-cloak>
<div ng-controller="contractController" ng-cloak>
<div data-ng-if="item" class="panel panel-default">
<div class="panel-heading text-center">
<span class="h3">Contract</span>
</div>
</div>
</div>
<button data-ng-if="item && !ajaxOut" class="btn btn-success" data-ng-click="saveItem()">Save</button>
</div>
Run Code Online (Sandbox Code Playgroud)
saveItem() 通过按钮调用,代码在制造商控制器中:
$scope.saveItem = function () {
$scope.ajaxOut = true;
$scope.saveContracts();
};
Run Code Online (Sandbox Code Playgroud)
但是函数 saveContracts() 在 contractController 中。我想从manufacturerController.saveItem() 调用contractController.saveContracts()。
根据这里我应该能够很好地调用父方法:How to call a function from another controller in angularjs?
但是保存会冻结浏览器。我做错了什么,我该如何解决?
在ControllerOne中
$scope.displayReport = function( status ){
statusCkeck( status ) ;
}
Run Code Online (Sandbox Code Playgroud)
statusCkeck() ; 在另一个控制器 - 控制器2
如何将此功能从一个控制器调用到另一个控制器
我试过了 angular.element(document.getElementById('controllerTwo')).scope().statusCheck();
但它抛出错误.
请建议
angularjs ×13
javascript ×8
angular-ui ×1
controller ×1
highcharts ×1
html ×1
html5 ×1
ngresource ×1