Solutions to some of the problems encountered with Angularjs

Source: Internet
Author: User

1. Angularjs $http.post request, SPRINGMVC background cannot receive parameter value solution

The problem is generally:Required String parameter ' Rpassword ' is not present or other 400 error

Workaround One: Modify the source http://my.oschina.net/buwei/blog/191640 (you can learn more about why the request is not received)

Solution Two: Feel modified source code is always bad, you can use this method, modify the submission parameter config headers and transformrequest

(1) Create a global transformrequest function

var app = Angular.module (' myApp '); app. Config (function  ($httpProvider)    { function (data) {        if (data = = = undefined            ) {return  data;        }         return $.param (data);    }});

Then for each method or create a global Content-type header

$httpProvider. defaults.headers.post[' content-type '] = ' application/x-www-form-urlencoded; Charset=utf-8 ';

(2) Create a local transformrequest for each required method

var function (data) {        return  $.param (data);    }    $http. Post ("/foo/bar", RequestData, {        ' content-type ': ' application/x-www-form-urlencoded; Charset=utf-8 '},        transformrequest:transform    }). Success (function( ResponseData) {        //dostuff with response    });

Reference URL Angularjs-any $http. Post to send request parameters instead of JSON?

2. use of scope. $apply

The general problem is: Data two-way binding invalidation, is clearly in the controller inside give $scope.xxx assignment, on the page xxx leng display, but click on the input box or form form of the submit button, XXX data information shows, is not good pit ah.

Workaround: Add $scope. $apply ()

For example

$scope. $apply (function() {   = "The value you have assigned"; 
});

Reason

In general, we do not need to add this code manually, because the ANGULARJS itself is called when needed in order to achieve the effect we see the data two-way binding.

But if you refer to an external plug-in or other, create or update $scope in the callback function . XXX data, because the external plug-in itself has been out of the scope of ANGULARJS, so data bidirectional binding here is not effective, can only manually add $scope. $apply () to inform the page to get the data.

3. Because it is a novice, step-by-step to

Solutions to some of the problems encountered with Angularjs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.