传递价值之间的角度JS控制器和服务 [英] Pass value in-between angular JS controller and services
问题描述
我试图用得到来自HTML的用户查询 NG-点击
。我想用这是我从NG-点击获取价值做出HTTPS调用。我可以在警报查看数据 - 1 但在警报 - 2 我得到未定义
。互联网上,我读了使用的服务是最好的practice.Please纠正我,如果我错了。
I am trying to get the user query from html using ng-click
. I want to make a https call using the value which I fetch from ng-click. I can see the data in Alert--1 but in Alert--2 i get undefined
. on internet I read that passing values using services is the best practice.Please correct me if I am wrong.
我的控制器
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"
})
有没有更简单的方法?
Is there any simpler way?
推荐答案
使用服务就可以了。看看这个,是begginers相当明确:
Using a service is OK. Take a look at this, is quite clear for begginers:
https://egghead.io/lessons/angularjs-sharing-data-控制器间
警报(警报 - 2'+ searchStr);
,因为它正前 $ scope.getQuery $执行是显示未定义C $ C>明显。控制器的初始化
NG-INIT
评估前pression之前完成。
alert('Alert--2'+searchStr);
is showing undefined because it is being executed before $scope.getQuery
obviously. Controller's initialization is done before ng-init
evaluates the expression.
在你的情况下,我相信这是好当数据被设置为触发事件,那么第二个控制器得到通知。这是正在与 $做了
和 $发出
。
In your case I believe it is better to fire an event when the data is set, so the second controller gets notified. This is being done with $on
and $emit
.
下面是您的例子plunker: http://plnkr.co/edit / 97mVwbWmoOH3F7m8wbN0?p = preVIEW
Here is a plunker with your example: 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;
}
};
});
这篇关于传递价值之间的角度JS控制器和服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!