0

In AngularJS I have input that is populated by php like so:

<form method="post" ng-controller="PostCtrl">
 <input type="text" name="post_title" ng-model="post_title" />
</form>

Now in my Controller, I never to specify ng-model, so I lets imagine I have empty controller:

EngagementApp.controller('PostCtrl', ['$scope',
function($scope) {

    return function($scope) {

    }
}]);

The problem I am having is that anything tied to an ng-model automaticcaly gets replaced with an empty value. My question is how can I stop the default php value from being replaced if no value in the controller is set?

1
  • Set the default value in Angular ng-model="<?php $blah ?>" Commented Oct 14, 2013 at 15:36

2 Answers 2

2

You can use ng-init to set an inital value. For example

<input type="text" name="post_title"
 ng-model="post_title" ng-init="post_title='<?$=phpValue;?>'" />

The HTML value attribute is ignored by AngularJS.

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

1 Comment

If you are supporting users without JavaScript, you will still need to set the value attribute. Also, it's good practice to set the scope variable as @gion_13's answer recommends -- I normally initialize it to an empty string.
0

You should could the default values from your controller:

$scope.post_title = 'default value';

But initializing the model from the html by using ng-init ( like @Thomas suggested ) makes more sense in this particular case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.