离子角 单击按钮后更新$scope变量
Ionic angular Update $scope variable after click a button
我想在单击保存按钮的那一刻更新我的$scope变量,基本上我正在尝试更新配置文件名称,所以我需要当我按下保存按钮时,这个新名称立即显示在图像配置文件旁边的导航栏中,我尝试了以下方法...但对我不起作用,我确实使用了$apply:
控制器.js
.controller('ProfileCtrl', function($ionicPlatform, $cordovaMedia, $scope, Words, Utilities, $timeout) {
$scope.save = function(){
var content = toTitleCase($scope.editWordRow.title);
//$scope.profilename = '';
$timeout(function () {
$scope.$apply(function () {
$scope.profilename = content;
console.log('profile name tiene esto, deberia mostrarse al salir de guardar: ' + $scope.profilename);
});
}, 2000);
};
// the follow $scope function is called when the controller starts
$scope.getProfileRow = function() {
Words.getProfileSimple().then(function(single){
if (single === null || single === undefined) {
$scope.editWordRow = {
title: null,
congrats_path: null,
name_sound_path: null,
profile_image_path: null
};
}
else
{
$scope.editWordRow = single;
$scope.profilename = single.title;
}
});
};
});
菜单.html
<ion-nav-buttons side="left">
<button class="button button-icon button-clear ion-navicon" menu-toggle="left"></button>
<div style="margin: 0 auto; width:30px" ><img src="img/iconos/logo.png" width="30px" height="30px"></div>
<div ng-controller="ProfileCtrl">
<h4 style="color:#fff !important; margin: 5px !important;" >{{profilename}}</h4>
</div>
</ion-nav-buttons>
调用 save() 函数的视图为:
inicio2.html
<ion-nav-buttons side="right">
<button class="button button-icon button-clear ion-ios-checkmark-outline" ng-click="save()"> </button>
</ion-nav-buttons>
如果我在另一个视图中调用 save() 函数有问题?
谢谢,问候我将不胜感激任何帮助!
对于典型的应用程序,导航栏、页脚在所有视图中共享,如果需要共享页眉、页脚中的范围值并使它们在应用中相同,则可以使用 $rootScope.profilename
分配它们。这样,每当您更新$rootScope.profilename
时,它都会反映在整个应用程序中。
如果要
更新不受当前控制器控制的视图中的值,可以通过两种方式执行此操作:
- 触发事件并在实际控制菜单的正确控制器上捕获它。
- 使用作为两个控制器的依赖项的服务,并将变量的值存储在该服务中。
编辑:
如果变量"profileName"附加到 2 路数据绑定的范围,然后在用户输入时更改,您可以执行以下操作:
$scope.$watch('profileName', function(changedProfileName) {
$scope.$emit('profileName:changed', changedProfileName);
});
然后,在导航栏控制器中,可以捕获事件:
$rootScope.$on('profileName:changed', function(e, newProfileName) {
$scope.profileNameToShow = newProfileName;
});
相关文章:
- ng在更新$scope后重复不更新信息
- 获取Div Width,然后更新Scope Variable
- 在从websocket服务器推送消息后更新$scope变量
- JavaScript未更新$scope变量的一部分
- 如何在Angular中传递此回调并更新$scope
- 离子角 单击按钮后更新$scope变量
- AngularJS:如何在承诺响应后更新$scope
- AngularJS更新$scope变量并输出到pre
- 从 AngularJS 中的服务属性更新$scope
- 更新$scope-dons'不要影响视图
- AngularJS控制器在后端数据更改时自动更新$scope
- 从AngularJS中的服务获取数据后,如何更新$scope是最好的方法
- Angular js 在
标签上的变量更新$scope动画
- 为什么在这种特殊情况下视图没有更新?($scope.$apply)
- 通过更新$scope模型更正条目后,清除角度形式验证
- 当contenteditable Elements使用Angular.js更改时,更新$scope
- Angularjs-在$resource-delete方法的回调中更新$scope的问题(thinkster.io第三章
- Angular JS,在指令中从控制器中更新$scope
- 关闭模式时更新$scope集合
- Ionic:更新$scope变量后重新加载视图