angularjs - How to assign scope in a promise? -
I want to allocate one value in one of my scope (one in http.get) but my value is not assigned to my For the scope! I have tried many things (use my scope instead of this in controller) but it does not work: (
Here's my HTML search input:
< Code> & lt; input class = "searchcontent" type = "text" placeholder = "Type here to search for content" ng-controller = "search as search" ng-keyp = "search. Here is my view for my search page:
{/ search.scope.results} }}This is my JS code:
(function () {var app; app = angular. Module ('Search Module', [] ); App.controller ('SearchCtrl', ['$ location', '$ http', function ($ location, $ http) {var is this = this; this.typingQuery = function () {var url = This.scope Url; if (url! = '' & Amp; url! = Undefined) {if (url == 'profile' || Url == 'Education' || Url == 'Skills' || Url == 'experience' || Url == 'Contacts') $ location.path (url); Else {$ location.path ('Search /' + url); $ Http.get ('/json/profile.json') .success (function) {var re = New RegExp (url); var results = data. Section [0] .body.match (again); This scope .results = results;}) .error (console.log ('error: failed to load json')); }} And $ location.path ('profile'); }; }]; }) ();
Thank you in advance!
In Angul-land, you do not use "this" too much (which is a relief because It always saves in many contexts to get a reference like "this" or "self" as we always used to).
Instead, you allocate data across the scope of your controller and you always have a good reference for it. But you have to get the context by asking for it. If you do not do this, then the $ scope will not really be what you think it is. You will end up with the classic JS mistake of creating a global variable when you put something in it (or throw an exception, depending on what you do).
You have a scope of $ controller to inject the anglor so that you can work with:
< P> And in your template, you can make them easier:app.controller ('searchCtrl', ['$ scope ',' $ Location ',' $ http ', function ($ scope, $ location, $ http) {// You can now safely / correctly use $ scope.XYZ here. Usually // define both data and functions: $ scope.results = ''; $ scope.typingQuery = function) {// ...} // ...}
& lt; Input class = "SearchContent" ... ng-keyz = "typing circle ()" ng-model = "url" & gt; Note that you do not usually use
the way the keyword controller takes you - this can be done, but it can be done quickly Gets confused and may have difficulty in debugging. You usually wrap something like a search block when you do it in an external DIV layer, then no one is required to hack this now:
& lt; Div class = "search-box" ng-controller = "searchCtrl" & gt; & Lt ;! - Find input boxes, outputs, buttons, etc. - & gt; & Lt; Input class = "search communication" type = "text" placeholder = "type here to search for content" ng-keyp = "typing ()" ng-model = "url" & gt; & Lt; / Div & gt;
It's basically the same, but you type little less (unless your search components are closed together).
Comments
Post a Comment