I have run into a issue when trying to reference models with angularjs. I can get some of them to reference, but others are not working. I've included the code below:
function DeleteCustomers($scope, func) {
$scope.delete = function() {
func.deleteCustomer($scope.customer);
}
}
function UpdateCustomers($scope, func) {
$scope.update = function() {
func.updateCustomer({ name: $scope.name2, telephone: $scope.telephone2,
email: $scope.email2, street: $scope.street2,
city: $scope.city2, zipcode: $scope.zipcode2 });
}
}
}
And the html is
<div ng-controller="UpdateCustomers">
<form class="test-angular-update">
<input type="text" ng-model="name2"><br>
<input type="text" ng-model="telephone2"><br>
<input type="text" ng-model="email2"><br>
<input type="text" ng-model="street2"><br>
<input type="text" ng-model="city2"><br>
<input type="text" ng-model="zipcode2"><br>
<button class="button" ng-click="update()">Update</button><br>
</form>
</div>
<br><br>
<div ng-controller="DeleteCustomers">
<form class="text-angular-delete">
Customer Name <input type="text" ng-model="customer"><br>
<button class="button" ng-click="delete()">Delete</button><br>
</form>
</div>
All the models from UpdateCustomers and DeleteCustomers are not being able to be referenced by the controllers. Any help would be appreciated.
Thanks!
asked Feb 23, 2013 at 1:50
user2101411
1,2003 gold badges15 silver badges37 bronze badges
-
1It's not quite clear what your problem is, and your code is too incomplete to make a guess. Could you provide a jsFiddle that demonstrates the issue better?Anders Ekdahl– Anders Ekdahl2013年02月23日 07:17:41 +00:00Commented Feb 23, 2013 at 7:17
-
The problem is that the ng-models under UpdateCustomers and DeleteCustomers are not being resolved. Thus, I cannot access them via $scope like I can with the other models.user2101411– user21014112013年02月23日 13:34:26 +00:00Commented Feb 23, 2013 at 13:34
1 Answer 1
Here's a more solid application structure, which might help ensure the func service is injected properly. Not sure if that's the issue you're having.
//app.js
var app = angular.module('app', []);
//services.js
app.factory('func', function(){/* service code*/});
//controllers.js
app.controller('DeleteCustomers', ['$scope', 'func', function($scope, func){
$scope.delete = function() {
func.deleteCustomer($scope.customer);
};
}]);//edit had typo here missing ']'
EDIT: Working demo http://jsfiddle.net/grendian/Um3pR/
answered Feb 23, 2013 at 18:38
grendian
4,1581 gold badge15 silver badges7 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
user2101411
this is what I put
Module.controller('DeleteCustomers', [ '$scope', 'func', function($scope, func) { $scope.delete = function() { func.deleteCustomer($scope.customer); }; }]); no luck.grendian
Edited original to fix type and include working demo. See if that helps.
default