javascript - Add buttons to html template, after receiving JSON -
javascript - Add buttons to html template, after receiving JSON -
i want add together buttons dom dynamically, after receiving json api when users access site. implemented function data, have problems add together buttons dynamically html template. furthermore, don't know how phone call function on first page load specific template.
here code:
app.run(function($rootscope, $http, apiservice) { $http.get('connection.properties').then(function (response) { apiservice.getallelements($rootscope); }); }); in apiservice have function:
getallelements : function($scope) { var requestdata = { 'username' : 'user123' }; this.dorequest(requestdata).success(function(data) { if (data.success) { console.log(data); // add together buttons } }).error(function() { alert('sorry bro.'); }); } i finish angularjs beginner, don't know right way on how this. help appreciated.
edit:
data json construction looks this:
{"success":true,"msg":"user active,1 element in db","userid":"1","elements":[{"element_id":"1","name":"element1","description":"this element 1"}]} and in profile.html template have static button:
<div class="btn-group btn-group-elements"> <button type="button" class="btn btn-default" ng-model="element" ng-change="save(true)" btn-radio="'1'">element1</button> </div>
could have multiple "elements" in info ? in case, ng-repeat great :
initialize elements : (in controller)
$scope.elements = []; link template $scope angularjs native directives :
<div class="btn-group btn-group-elements"> <button ng-repeat="element in elements" type="button" class="btn btn-default" ng-click="dosomething( element, $index )">{{ element.name }}</button> </div> fetch info :
getallelements : function($scope) { var requestdata = { 'username' : 'user123' }; this.dorequest(requestdata).success(function(data) { if (data.success) { $scope.elements = data.elements; // add together buttons } }).error(function() { alert('sorry bro.'); }); } javascript jquery html json angularjs
Comments
Post a Comment