0

I want to bind my form on the server so that I don't have to wait for a JavaScript model to be loaded and bound separately.

So from this starting point, how would I get a Javascript model in memory from the form elements such that Angular JS can work as per normal?

1 Answer 1

1

You can use ngResource, which can be used to bind the results from your service layer. It would look something like this:

HTML

<body ng-controller="MainCtrl">
    <p>Hello {{person.name}}!</p>
    <form name="myForm"><input name="name" ng-model="person.name"/></form>
</body>

Javascript

angular.module('plunker', ['ngResource'])
.controller('MainCtrl', function($scope, $resource) {
    var Person = $resource('/persons/:personId', {personId:@personId});
    $scope.person = Person.get({personId:1});
});

You can read more about ngResource here.

Sign up to request clarification or add additional context in comments.

3 Comments

I want to bind the form on the server though so that I don't have to do a call back to retrieve the data. I suppose I could embed the javascript in the page.
aha. I didn't get that. Yes, you could simply inject that data into the page. I will update my answer.
I would embed the JS on the page. Sure you could render the form serverside but then you wouldn't be able to do anything nice with it in angular without grabbing that information out of the DOM. I'm also confused when you say in your original question that you don't want to 'wait'. What exactly is causing you to have to wait?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.