How to access a template control value in parent - angularjs

we have controller which has ng-include to load an external html. now i want to access a control value in parent.
my template is MainMenuCreate
#{
Layout = null;
}
<div >
<h2>{{Headtitle}}</h2>
<p>
<div style="width:200px;">MainMenuId :</div>
<input type="text" name="MainMenuId" id="MainMenuId" data-ng-model="MainMenuId" />
</p>
<p>
<div style="width:200px;">MainMenu :</div>
<input type="text" name="MainMenu" id="MainMenu" data-ng-model="MainMenu" />
</p>
<p>
<div style="width:200px;">PageTitle :</div>
<input type="text" name="PageTitle" id="PageTitle" data-ng-model="PageTitle" />
</p>
<input type="button" name="btnSave" id="btnSave" value="Save" ng-click="Save()" />
and my main page is
<div data-ng-app="MainMenuModule">
<div data-ng-controller="MainMenuController">
<div data-ng-include="'MainMenuCreate'" >
</div>
{{MainMenuId}}
</div>
</div>
and controller is
app.controller('MainMenuController', function ($scope, MainMenuService) {
$scope.Save = function () {
var MainMenuRecord =
{
MainMenuId: $scope.MainMenuId,
MainMenu: $scope.MainMenu,
PageTitle: $scope.PageTitle
};
var promisePost = MainMenuService.post(MainMenuRecord);
promisePost.then(function (pl) {
$scope.MainMenu = pl.data.MainMenu;
loadRecords();
}, function (err) {
console.log("Err" + err);
});
}
});
when i call this save method then $scope.MainMenuId shows undefined

<div style="width:200px;">MainMenuId :</div>
<input type="text" name="MainMenuId" id="MainMenuId" data-ng-model="obj.MainMenuId" />
<div style="width:200px;">MainMenu :</div>
<input type="text" name="MainMenu" id="MainMenu" data-ng-model="obj.MainMenu" />
<div style="width:200px;">PageTitle :</div>
<input type="text" name="PageTitle" id="PageTitle" data-ng-model="obj.PageTitle" />
</div>
<input type="button" name="btnSave" id="btnSave" value="Save" ng-click="Save(obj)" />
app.controller('MainMenuController', function ($scope, MainMenuService) {
$scope.obj = {};
$scope.Save = function (obj) {
var MainMenuRecord =
{
MainMenuId: obj.MainMenuId,
MainMenu: obj.MainMenu,
PageTitle: obj.PageTitle
};
var promisePost = MainMenuService.post(MainMenuRecord);
promisePost.then(function (pl) {
$scope.MainMenu = pl.data.MainMenu;
loadRecords();
}, function (err) {
console.log("Err" + err);
});
}
});

Related

How to bind data in table using AngularJs?

I have a Simple form like Below
<form novalidate name="f1" ng-submit="SaveData(User)">
<b>Fname</b><input type="text" ng-model="User.fname" /><br />
<b>Lnamew</b><input type="text" ng-model="User.lname" /><br />
<input type="submit" />
</form>
And my controller is
app.controller('HomeCtrls', function ($scope) {
$scope.msg = "Ok...."
$scope.Getdata = [];
$scope.SaveData = function (data) {
$scope.Getdata.push(data)
}
})
Here i wana to bind Getdata in table Like
<tr ng-repeat="formdata in Getdata">
<td>{{formdata.User.fname}}</td>
<td>{{formdata.User.lname}}</td>
this is your answer with a little changes
var app = angular.module('formApp',[]);
app.controller('formController',function($scope){
$scope.Getdata = [];
$scope.SaveData = function (data) {
$scope.Getdata.push({
fname:$scope.fname,
lname:$scope.lname
})
console.log($scope.Getdata)
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="formApp" ng-controller="formController">
<form novalidate name="f1" ">
<b>Fname</b><input type="text" ng-model="fname" /><br />
<b>Lnamew</b><input type="text" ng-model="lname" /><br />
<button type="button" ng-click="SaveData()">Save</button>
</form>
</div>

unable to get data from html in angular js

in this code of HTML, we get input text value and send to the Angular controller
so they get to work as defined in code.
<div class="row" ng-controller="RegionController">
<div class="col-lg-12" >
<div class="hpanel">
<div class="panel-heading">
<!-- <div panel-tools></div> -->
<h2>Region Master Entry</h2>
</div>
<div class="panel-body">
<!--change form name,and submit controller name-->
<form role="form">
<div class="form-group">
<label class="col-sm-2 control-label">Region Name</label>
<div class="col-sm-10">
<input type="text" placeholder="please enter Region name" class="form-control m-b" required name="Region Name" ng-model="formRegionData.region_name" >
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Region Code</label>
<div class="col-sm-10">
<input type="text" placeholder="please enter Region code" class="form-control m-b" required name="Region Code" ng-model="formRegionData.region_code">
</div>
</div>
<div class="form-group">
<div class="col-sm-3">
<label>Active</label>
</div>
<div class="checkbox checkbox-success col-sm-9">
<input id="checkbox3" type="checkbox" checked="" ng-model="formRegionData.status">
<label for="checkbox3">
</label>
</div>
</div>
<div class="form-group">
<div class="col-sm-4"></div>
<div class="col-sm-8">
<button class="btn btn-sm btn-primary btn-xl text-right" type="submit" ng-click="createRegion()"><strong> Save Region </strong></button>
</div>
</div>
{{formRegionData | json}}
</form>
</div>
</div>
</div>
</div>
"{{formRegionData | json}}" this will return in HTML input text result of but not send data to the controller
in Controller the code is written as
.controller('RegionController', function( $scope , regionService) {
$scope.createRegion = function() {
debugger;
vm.processing = true;
vm.message = '';
console.log(formRegionData);
regionService.SaveRegion( formRegionData )
.then(function(data) {
debugger;
//console.log(data);
//.success(function(data) {
vm.processing = false;
vm.storyData = {};
vm.message = data.message;
});
}
})
and my service to work according to Controller
.factory('regionService',function($http ){
var regionFactory = {};
regionFactory.SaveRegion = function(formRegionData) {
debugger;
return $http.post('/api/region/', formRegionData);
}
return regionFactory;
});
You have missed $scope
$scope.createRegion = function() {
debugger;
$scope.processing = true;
$scope.message = '';
console.log($scope.formRegionData);
regionService.SaveRegion($scope.formRegionData )
.then(function(data) {
debugger;
//console.log(data);
//.success(function(data) {
$scope.processing = false;
$scope.storyData = {};
$scope.message = data.message;
});
}
})
and remove ng-click="createRegion()" in your button control and add this code in your form tag by ng-submit. like,
<form ng-submit="createRegion()" role="form">
You have missed of $scope in form region data
Here is the link Jsfiddle
JS
angular.module('myApp', ['ngStorage'])
.controller('RegionController', function($scope, regionService) {
var vm = this;
$scope.createRegion = function() {
debugger;
vm.processing = true;
vm.message = '';
regionService.SaveRegion($scope.formRegionData)
.then(function(data) {
debugger;
//console.log(data);
//.success(function(data) {
vm.processing = false;
vm.storyData = {};
vm.message = data.message;
});
}
}).factory('regionService', function($http) {
var regionFactory = {};
regionFactory.SaveRegion = function(formRegionData) {
debugger;
return $http.post('/api/region/', formRegionData);
}
return regionFactory;
});

Fields values generated using ng-repeat is not getting while submit

Template for form submission. This page will display the form template. Initially it shows the TItle,Full Name. On clicking the 'Add Tags' link new input fields is been generated for entering tags.
On submit, the field input(story.tag) is not been included on RequestPayload
<form novalidate ng-submit="save()">
<div>
<label for="title">Title</label>
<input type="text" ng-model="story.title" id="title" required/>
</div>
<div>
<label for="firstName">Full Name</label>
<input type="text" ng-model="story.fullname" id="fullname" required/>
</div>
<div ng-controller="Note" >
<div ng-repeat="story in items ">
<label>Tag {{$index+1}}:</label>
<input type="text" ng-model="story.tag" id="tag" required/>
</div>
<a ng-click="add()">Add Tags</a>
</div>
<button id="save" class="btn btn-primary">Submit Story</button>
</form>
script :- app.js
angular.module("getbookmarks", ["ngResource"])
.factory('Story', function ($resource) {
var Story = $resource('/api/v1/stories/:storyId', {storyId: '#id'});
Story.prototype.isNew = function(){
return (typeof(this.id) === 'undefined');
}
return Story;
})
.controller("StoryCreateController", StoryCreateController);
function StoryCreateController($scope, Story) {
$scope.story = new Story();
$scope.save = function () {
$scope.story.$save(function (story, headers) {
toastr.success("Submitted New Story");
});
};
}
//add dynamic forms
var Note = function($scope){
$scope.items = [];
$scope.add = function () {
$scope.items.push({
inlineChecked: false,
tag: "",
questionPlaceholder: "foo",
text: ""
});
};
}
The story object inside ng-repeat is in another scope. This JSFiddle should do what you are looking for.
<div ng-app>
<div ng-controller="NoteCtrl">
<form novalidate ng-submit="save()">
<div>
<label for="title">Title</label>
<input type="text" ng-model="story.title" id="title" required/>
</div>
<div>
<label for="firstName">Full Name</label>
<input type="text" ng-model="story.fullname" id="fullname" required/>
</div>
<div ng-repeat="story in items">
<label>Tag {{$index+1}}:</label>
<input type="text" ng-model="story.tag" required/>
</div> <a ng-click="add()">Add Tags</a>
<button id="save" class="btn btn-primary">Submit Story</button>
</form>
<div ng-repeat="test in items">
<label>Tag {{$index+1}}: {{test.tag}}</label>
</div>
</div>
</div>
The NoteController:
function NoteCtrl($scope) {
$scope.story = {};
$scope.items = [];
$scope.story.tag = $scope.items;
$scope.add = function () {
$scope.items.push({
inlineChecked: false,
tag: "",
questionPlaceholder: "foo",
text: ""
});
console.log($scope.story);
};
}

Firebase angularfire child.$asObject give properties undefined

I've got this code:
factory
app.factory('Items', function($firebase,FIREBASE_URL) {
var ref = new Firebase(FIREBASE_URL);
var items = $firebase(ref.child('items')).$asArray();
var Item = {
all: function () {
return items;
},
create: function (item) {
return items.$add(item);
},
get: function (itemId) {
return $firebase(ref.child('items').child(itemId)).$asObject();
},
update: function (itemId, item) {
return $firebase(ref.child('items').child(itemId)).update(item);
},
delete: function (item) {
return items.$remove(item);
}
};
return Item;
});
route
app.config(function($stateProvider) {
$stateProvider
.state('items_update', {
url: '/items/update/:id',
templateUrl: 'views/items/form.html',
controller:'ItemsUpdateController',
resolve:{
item:function(Items,$stateParams){
return Items.get($stateParams.id);
}
}
})
});
controller
app.controller('ItemsUpdateController', function ($scope, item, $state) {
$scope.item = item;
console.log($scope.item.url);
$scope.add = function() {
Items.update($scope.item).then(function () {
$state.transitionTo('items');
});
}
});
why console.log($scope.item.url); give me undefined ?
but in the view I've got all the data
html
<form class="form-horizontal" role="form" name="form" data-ng-submit="add()">
<div class="form-group">
<input type="text" name="title" tabindex="1" class="form-control" placeholder="{{ 'items.form.title' | translate }}" data-ng-model="item.title" required="required" data-ng-minlength="3" data-ng-maxlength="25" user-feedback />
</div>
<div class="form-group">
<input type="text" name="ingredients" tabindex="2" class="form-control" placeholder="{{ 'items.form.ingredients' | translate }}" data-ng-model="item.ingredients" required="required" ng-pattern="/^\w(\s*,?\s*\w)*$/" user-feedback />
</div>
<div class="form-group">
<input type="text" name="price" tabindex="3" class="form-control" placeholder="{{ 'items.form.price' | translate }}" data-ng-model="item.price" required="required" data-smart-float user-feedback />
</div>
<div class="form-group">
<button type="button" tabindex="4" class="btn btn-default btn-lg" item="item" data-uploader>{{ 'items.form.upload' | translate }}</button>
<input type="text" name="url" style="display:none;" required="required" data-ng-model="item.url" />
</div>
<div class="form-group form-no-required clearfix">
<div class="pull-right">
<button type="submit" tabindex="5" class="btn btn-primary" data-ng-disabled="form.$invalid">{{ 'items.form.submit' | translate }}</button>
</div>
</div>
</form>
ENDS UP
as in the #Frank van Puffelen comment
I worked it out with:
app.controller('ItemsUpdateController', function($scope, item, $state) {
item.$loaded().then(function(data) {
$scope.item = data;
console.log($scope.item.url);
});
$scope.add = function() {
Items.update($scope.item).then(function() {
$state.transitionTo('items');
});
}
});
This is because by the time your console.log($scope.item.url); runs, the data hasn't been loaded from Firebase yet. Angular listens for a notification from Firebase/AngularFire to know when the data has loaded and then updates the view.
Also see angularfire - why can't I loop over array returned by $asArray? and Trying to get child records from Firebase.

Angular Form not submitting on first click

I have built a small angular App into my website whereby a user enters a searchterm into an input field and then values are returned via an Angular service. When I attempt to submit a value however, the form does not submit and will only submit on the 2nd attempt. I cannot figure out why this is happening.
Here is my code:
<div ng-app="clubFilter" class="col-lg-12">
<div class="col-lg-3">
</div>
<div class="col-lg-9" ng-controller="clubController">
<form ng-submit="filterClubs()">
<input type="text" name="location" ng-model="searchTerm" placeholder="Search..." />
<input type="submit" name="submit" value="Submit" />
</form>
<ul class="leisure-centres">
<li ng-repeat="club in clubs">
<div class="centre">
<a class="link" ng-href="{club.link}">More info</a>
<div class="image" ng-show="club.image > 0">
<img src="{{image}}" alt="{{club.title}}" />
</div>
<div class="details">
<div class="title">
<h3>{{club.title}}<span ng-show="club.distance > 0"> - {{club.distance}} miles away</span></h3>
</div>
<div class="address">
{{club.building}},
{{club.street}},
{{club.city}},
{{club.county}},
{{club.postcode}}
</div>
<div class="tel">
<strong>Tel: </strong>
</div>
<div class="email">
<strong>Email: </strong>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
var JSONItems = <?php echo $this->JSONItems; ?>;
var searchTerm = "<?php echo $this->searchTerm; ?>";
And here is my angular controller
angular.module('clubFilter.controllers', []).
controller('clubController', function($scope, $http, googleMapService) {
$scope.keyWord = "SEARCH";
$scope.clubsJSON = JSONItems;
if(searchTerm == "") {
$scope.clubs = $scope.clubsJSON;
} else {
$scope.searchTerm = searchTerm;
googleMapService.setLatLng($scope.searchTerm, $scope.clubsJSON).then(function(sortedArray) {
$scope.$apply(function() {
$scope.clubs = sortedArray;
});
}, function(err) {
alert("no");
});
}
$scope.filterClubs = function() {
googleMapService.setLatLng($scope.searchTerm, $scope.clubsJSON).then(function(sortedArray) {
$scope.clubs = sortedArray;
}, function(err) {
alert("no");
});
}
});
As far as I am aware I have everything defined as it should be?
Thanks
Try this one;
<div class="col-lg-9" ng-controller="clubController as club">
<form ng-submit="club.filterClubs()">
<input type="text" name="location" ng-model="club.searchTerm" placeholder="Search..." />
<input type="submit" name="submit" value="Submit" />
</form>
And in your controller;
this.filterClubs = function() {
googleMapService.setLatLng(this.searchTerm, $scope.clubsJSON).then(function(sortedArray) {
$scope.clubs = sortedArray;
}, function(err) {
alert("no");
});
}

Resources