javascript - Angular how to set scope variable from service in right way? -
i homecoming value service method scope using , processing in template.
but found, cannot utilize scope in services. can utilize rootscope think, not right approach.
how can set value service scope?
thanks advice.
here code:
/** * init autocomplete dropdown menu project list */ this.getprojectlistforautocomplete = function (container, options) { $("#autocompleteprojects").kendoautocomplete({ datasource : { type: "json", serverfiltering: true, transport: { read: function (options) { console.log("list"); console.log(options.data); apiservice.dohttprequest( "post", $rootscope.apibaseurl + "gpsaddress/search", requestparams ) .success(function (data, status, headers, config) { break; } }) .error(function (data, status, headers, config) { }); } } }, datatextfield: "city" , datavaluefield: "address.city", filter: "contains", minlength: 1, alter : function (e) { console.log("change"); //console.log(e); }, select : function (e) { console.log("select"); var dataitem = this.dataitem(e.item.index()); console.log(dataitem); // here need set scope in controller } }); };
please see demo below:
class="snippet-code-js lang-js prettyprint-override">var app = angular.module('app', []); app.service('dataservice', function() { var _person = { name: "jack", surname: "doe" } homecoming { person: _person } }) app.controller('fctrl', function($scope, dataservice) { $scope.person = dataservice.person; });
class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app"> <div ng-controller="fctrl"> <p>first name: {{person.name}}</p> <p>last name: {{person.surname}}</p> edit first name:<input type: "text" ng-model="person.name" /> </div> </div>
javascript angularjs scope
No comments:
Post a Comment