Sorry if this was already asked, but I really didn't find anything that applies exactly to my problem.
Well, although I know that an AngularJS form value must be loaded from controllers and that I can create routes and RESTful resources with AngularJS, I am using Python and Flask to get the page and data but AngularJS to post data to the server. The problem is, when I put "ng-model" within a text input, the value from server is ignored, like so:
<input type="text" ng-model="first_name" value={{user.first_name}}>
is interpreted as:
<input type="text" ng-model="first_name" value="John" class="ng-pristine ng-valid">
but the value cannot be seen, as if there is no "value" attribute.
Given that situation, I tried to use directives, but I didn't realise how to get this server data and share it with another scope.
What I want is to simply load a page with server data inside a ng-model input and access it through controller methods later ("placeholder" is useless here).
I am sending the page like this:
class ShowUser(MethodView):
def get(self):
s = Session(request)
if s.is_logged():
user = dict({'username': s.user.username, 'first_name': s.user.first_name, 'last_name': s.user.last_name,
'email': s.user.email, 'logged': True})
return render_template('users/show-user.html', user=user)
else:
user = dict({'username': 'null', 'logged': False})
return render_template('index.html', user=user)
It's that "user" variable in "render_template" that I am trying to access.
value
andng-model
together.ng-model
takes the place ofvalue
. If$scope.first_name
isn't being set in your controller than the input value will not change. Can you verifyfirst_name
is being set by whatever web service you are using? Also, iffirst_name
is being set, but it's outside the scope of angular, you'll want to wrap it in an$scope.$apply()
– Ronnie Jul 21 '14 at 17:23$scope.$apply('foo = {{user.first_name}}');
. I just don't know how to put a server-processed string inside a Angular controller scope. – Rodrigo Jul 21 '14 at 17:33first_name
in the response handler. And that will just do what you want. It'd be better if you can share how you are loading data from the server. – vaidik Jul 21 '14 at 17:40$http
call in your controller? That is what @vaidik was asking – Ronnie Jul 21 '14 at 18:16<span ng-bind="loadData({{user}})"></span>
, where "loadData" is a controller method, I can actually access the data. I am not doing $http.get to get this page, that is the problem. – Rodrigo Jul 21 '14 at 18:33