1

I'm developing an extension using angular. When the controller is load I need to charge information from the local storage :

var app = angular.module("Logins");

app.controller("LoginsCtrl", function($scope) {

    chrome.runtime.sendMessage({message: "get_data"}, function(response) {
        $scope.myText = response.value;
    });
});

And in the view a very simple :

{{ myText }}

If I init the value with a default value it is display properly, when I check the console.log, the value is correct. But the view is not refreshed.

I've seen some question about tick(), detectChanges() but I don't understand how to use those function, angular keep saying to me the function is undefined.

Can you help me ?

Thanks,

Regards,

Martin

  • can you post entire component? because in order to use detectChanges you have to inject `ChangeDetectorRef`. – piotr szybicki Nov 26 '18 at 08:25
  • I did not implement the changeDetectorRef because I didn’t understand how to use it inside my controller. I was just mentioning what I’ve found online – Martin Chalot Nov 26 '18 at 17:08

2 Answers2

0

As asked here is my full code :

popup.html :

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />

<script src="angular-1.6.9.min.js"></script>
<script src="controller.js"></script>
</head>
<body>
    <div ng-app="Logins" ng-controller="LoginsCtrl">

        {{ myText }}

    </div>
</body>
</html>

Here is controller.js :

var app = angular.module("Logins");

app.controller("LoginsCtrl", function($scope) {
    var getData = function(response, sender, sendResponse) {
        $scope.myText = response.value;
        console.log($scope.myText);
    };
    chrome.runtime.sendMessage({message: "get_data"}, getData.bind(this));
});

Here is background.js :

chrome.runtime.onMessage.addListener( function(request,sender,sendResponse) {
    if(request.message === "get_data") {
        sendResponse({value:"toto"});
    } else {
        console.log('Message unknown');
    }
});

So it's pretty basics.

Regards,

Martin

0

I found the answer, I made a mistake : $scope.apply() Au lieu de $scope.$apply()