D
D
dissdoc2015-12-30 18:57:52
Angular
dissdoc, 2015-12-30 18:57:52

Why is the view not being rendered in the Angular module?

Hello! I'm new to angularjs and I can't figure out what I'm doing wrong.
Actually, I use ui-router as a routing. And there is a problem out of the blue.
1. In the controller I use this code.
$scope.title = 'TEST';
I go to such a page using ui-sref='main'. Everything works well.
2. In the controller I add ajax request

function simple() {
    var deferred = $.defer();
    $http.get()... // какой-то элементарный запрос
    return deferred.promise;
}
// далее уже делаю что-то вроде
simple().then(function() {
    $scope.title = 'TEST';
});

As a result, if I refresh the browser page (F5), then everything is OK! If I pass us-sref='main', then the word 'TEST' is not shown. What to do? I've spent so much time on this question. I'm sure the answer is elementary

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-12-30
Protko @Fesor

What to do?

Read documentation, read best-practice, read about common mistakes.
1) controllerAs syntax. Necessarily. Don't use $scope in controllers at all.
2) Deal with promises. This is not relevant to the problem, but just... $http.get already returns a promise, why make a new one just like that. Well, again, since version 1.3, you can create promises in a more convenient way (the $q object has become a function).
So far, without seeing the real code and the whole picture, the first candidate is a problem with the inheritance of scalars in scopes (more precisely, scalars are not inherited and are simply overwritten in the current scope). If you tried somehow to transfer data from a state to another state through a scope, then you might have experienced something similar.

D
dissdoc, 2015-12-30
@dissdoc

1) Syntax met. Why not use $scope? How many tutorials I watch - $scope and $scope everywhere?
2) Understood
3) The code is now confusing. I'm ready to figure it out myself. In addition, you pointed out many errors at once. This is much more important. But I'll try to show you the code. It's already holidays, and only at night I will write)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question