I have GET request from the backend. In the directive link I need to check
data which I has bind to parent controller scope. It always should show
undefined. How can I solve this problem? Is there anyone can help?
Here is the code:
JS:
var myServiceApp = angular.module("MyServiceApp", []);
myServiceApp.factory('userListService', ['$http',
function($http) {
var doRequest = function(username, path) {
return $http({
method : 'GET',
url : 'users.json'
});
}
var data = "";
return {
userList: function(username) {
return doRequest(username, 'userList');
},
setData: function(inputData){
data = inputData;
},
getData: function(){
return data;
}
}
}
]);
myServiceApp.controller('ServiceController', ['$scope', '$timeout',
'userListService',
function($scope, $timeout, userListService) {
$scope.newUserName = "All";
if(userListService.getData() === ""){
userListService.userList($scope.newUserName)
.success(function(data, status) {
console.log(data);
userListService.setData(data);
$scope.users = data;
});
}else{
$scope.users = userListService.getData();
}
}
]);
myServiceApp.directive('userlist', ['$http', function($http){
// Runs during compile
return {
scope: {
userData: '=inputData'
},
restrict: 'EA',
template: '<ul><li ng-repeat="user in
userData">{{user.num}}</li></ul><p>{{reslut}}</p>',
link: function($scope, iElm, iAttrs, controller) {
*//I want to check the userData*
if(typeof($scope.userData)=="undefined"){
$scope.reslut = "undefined";*// but it always show this
result.*
}else{
$scope.reslut = $scope.userData;
}
}
};
}]);
html:
<html ng-app="MyServiceApp">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet"
href="framework/bootstrap-3.0.0/css/bootstrap.css">
<script src="framework/angular-1.3.0.14/angular.js"></script>
<script src="MyService1.js"></script>
</head>
<body>
<div ng-controller="ServiceController">
<label>username</label>
<userlist input-data="users"></userlist>
</div>
</body>
</html>
Here is the online site: http://plnkr.co/edit/O0W1VOv1k1fsyBeQsDsN?p=preview
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.