我正在尝试使用ng-click从html获取用户查询。我希望使用从ng-click获取的值进行https调用。我可以在Alert-1中看到数据,但在Alert-2中我得到undefined。在互联网上,我读到使用服务传递值是最好的practice.Please,如果我错了,纠正我。
我的控制器
mainApp.controller('searchController',function($scope,searchString){
$scope.getQuery = function(userq) // its ng-click="getQuery(userq)" on Search button
{
$scope.userq=userq;
alert('Alert--1'+userq); // its working fine
searchString.setSearchString(userq);
};
});
//====================
mainApp.controller('fetchQueryResultController',function($scope,searchString){
var searchStr = searchString.getSearchString();
alert('Alert--2--'+searchStr); // Undefined
// Later I'll use this value to fetch data from Watson search(Django) using $https call
});我的服务:
mainApp.factory('searchString', function () {
var qVal ;
return {
setSearchString:function (query) {
qVal = query;
},
getSearchString:function () {
return qVal;
}
};
});路由:
.when('/search', {
templateUrl: "../static/views/seachResult.html",
controller: "fetchQueryResultController"
})有没有更简单的方法?
发布于 2015-11-01 04:42:15
使用服务是可以的。看看这个,对于乞丐来说是很清楚的:
https://egghead.io/lessons/angularjs-sharing-data-between-controllers
alert('Alert--2'+searchStr);显示未定义,因为它显然是在$scope.getQuery之前执行的。控制器的初始化是在ng-init计算表达式之前完成的。
在您的情况下,我认为最好在设置数据时触发一个事件,这样第二个控制器就会得到通知。这是与$on和$emit一起完成的。
下面是一个例子:http://plnkr.co/edit/97mVwbWmoOH3F7m8wbN0?p=preview
var app = angular.module('plunker', []);
app.controller('searchController',function($scope,searchString){
$scope.searchText;
$scope.getQuery = function(userq) // its ng-click="getQuery(userq)" on Search button
{
$scope.userq=userq;
alert('Alert--1'+userq); // its working fine
searchString.setSearchString(userq, $scope);
};
});
//====================
app.controller('fetchQueryResultController',function($scope, $rootScope, searchString){
var searchStr = searchString.getSearchString;
$scope.getData = function(){
searchStr = searchString.getSearchString();
alert('Alert--2--'+ searchStr);
}
$rootScope.$on('dataModified', function(){
$scope.getData();
});
});
//====================
app.factory('searchString', function ($rootScope) {
var qVal ;
return {
setSearchString:function (query) {
qVal = query;
$rootScope.$emit('dataModified');
},
getSearchString:function () {
return qVal;
}
};
});发布于 2015-11-01 04:40:04
此警报未定义
var searchStr = searchString.getSearchString();
alert('Alert--2'+searchStr); 因为qVal还没有确定
qVal在getQuery被调用时设置,但该时间alert2已经执行
发布于 2015-11-01 09:51:58
一个简单的解决方案是让工厂返回一个对象,并让控制器使用对同一个对象的引用:
JS:
// declare the app with no dependencies
var myApp = angular.module('myApp', []);
myApp.factory('searchString', function() {
var qVal;
return {
setSearchString: function(query) {
qVal = query;
},
getSearchString: function() {
return qVal;
}
};
});
myApp.controller('setSearchController', function($scope, searchString) {
$scope.setQuery = function(userq) {
$scope.userq = userq;
searchString.setSearchString(userq);
};
});
myApp.controller('fetchQueryResultController', function($scope, searchString) {
$scope.getQuery = function(user) {
alert(searchString.getSearchString());
};
});HTML:
<div ng-app="myApp">
<div ng-controller="setSearchController">
<button ng-click="setQuery('Shohel')">Set</button>
</div>
<hr />
<div ng-controller="fetchQueryResultController">
<button ng-click="getQuery()">Get</button>
</div>
</div>这里有类似的小提琴
https://stackoverflow.com/questions/33459234
复制相似问题