不能共享的角度控制器变量 [英] Can not share variables in angular controllers
本文介绍了不能共享的角度控制器变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的角的js。我有两个HTML网页在两次NG-控制器。我想从一个控制器到另一个控制器共享数据。
下面是我创建的服务:
I am new to angular js. I have two ng-controller in two Html page. I want to share data from one controller into another controller. Here is the service i have created:
app.service('sharedProperties', function() {
var stringValue = 'test string value';
var objectValue = {
data: 'test object value'
};
return {
getString: function() {
return stringValue;
},
setString: function(value) {
stringValue = value;
},
getObject: function() {
return objectValue;
}
}
});
下面是控制器1:
app.controller('FirstCtrl',function($scope,sharedProperties){
sharedProperties.setString("Hi");
});
下面是控制器2:
app.controller('SeccondCtrl',function($scope,sharedProperties){
window.alert(sharedProperties.getString());
});
非但没有包含警报字符串'嗨'我得到'测试字符串值。
Instead of getting alert containing string 'Hi' i am getting 'test string value'.
NB。我在两个HTML文件中使用FirstCtrl和SeccondCtrl。而从FristCtrl设置字符串后,我重定向到另一个HTML文件。
推荐答案
I've created jsfiddle and it works fine.
app.controller('FirstCtrl',function($scope,sharedProperties){
$scope.set = function(){
sharedProperties.setString("Hi");
}
});
app.controller('SeccondCtrl',function($scope,sharedProperties){
$scope.get = function(){
window.alert(sharedProperties.getString());
}
});
这篇关于不能共享的角度控制器变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文