2017-01-13 1 views
0

속성 채널로 channelid라는 개체를 설정하려고합니다. 내가 그것을 액세스하려고 할 때 내가 그것을 액세스하려고하면 그러나 나는 객체 {}각도 서비스의 개체 속성에 액세스 할 수 없습니다.

서비스

function Service($http){ 
    var service = this; 
    var channelid = {}; 

    // Set channel ID 
    service.SetChannelID = function(id){ 
    channelid.channel = id; 
    }; 

    // Get Channel Id 
    service.GetChannelID = function(){ 
    return channelid; 
    }; 

컨트롤러

// Set channel id 
Service.SetChannelID(channel.id); 

// Retrieve channel id 
var channelID = Service.GetChannelID(); 

을 반환합니다. 경우 : { "C3NBGTQJD"채널을}

+1

'때문에

myApp.service('myService', function(){ var channelObj={}; var service = this; service.setChannelId = function(id) { channelObj.channel = id; }; service.getChannel = function() { return channelObj; } }); 

컨트롤러 'Service' 함수 인스턴스 ... You co uld는'getter'와'setter'를 통해서만이 값을 얻습니다. –

+0

'Service.SetChannelID'가 정의되어 있지 않습니다. 'SetChannelID'는 생성자 함수 자체가 아니라'Service' 인스턴스에서만 정의됩니다. – 4castle

+0

서비스는 클래스/생성자 함수입니다. 그것의 인스턴스를 만들었습니까? var service = 새 서비스(); 그런 다음 메서드를 호출합니까? – Hoyen

답변

0

console.log(channelID.channel); 

그것은 내가 인쇄하는 경우

정의되지 않은 인쇄

console.log(channelID); 

이 객체 {}

>Object 
    channel:"C3NBGTQJD" 
    __proto__: Object 

이 반대하지를 인쇄 참조하실 수 있습니다. 바이올린

"http://jsfiddle.net/ADukg/9158/" 

서비스 channelid`가 .. 당신은을 통해 액세스 할 수 없습니다 Service` 기능`의 개인 변수가

$scope.setChannelId = function (id) { 
     myService.setChannelId(id); 
} 

$scope.getChannelId = function() { 
     var channelObj = myService.getChannel() 
     alert(channelObj.channel); 
} 

템플릿

<div ng-controller="MyCtrl"> 
    <button ng-click="setChannelId(5)"> 
    Set Channel 
    </button> 
    <button ng-click="getChannelId()"> 
    Get Channel 
    </button> 

</div> 
+0

내가 뭘하려고하는지이 : var channelObj = myService.getChannel(); function GetChannelDataById (channelObj.channel); function GetChannelDataById (id) { // do something } – payucon

관련 문제