Javascript AngularJS中的归一化控制器
我对AngularJS很陌生。 我已经写了一个代码,它的工作很好。 想知道,有没有办法进一步缩小控制器。 我的index.html文件是Javascript AngularJS中的归一化控制器,javascript,php,angularjs,angular-http,Javascript,Php,Angularjs,Angular Http,我对AngularJS很陌生。 我已经写了一个代码,它的工作很好。 想知道,有没有办法进一步缩小控制器。 我的index.html文件是 <!DOCTYPE html> <html lang="en-US"> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<!DOCTYPE html>
<html lang="en-US">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<input type="text" ng-model="sea" ng-change="newSearch()"/>
<ul>
<li ng-repeat="x in myData">
{{ x.name + ', ' + x.emailid}}
</li>
</ul>
</div>
<script src="js/app.js"></script>
<script src="js/controllers/maincontroller.js"></script>
</body>
</html>
如果您注意到,我已经使用了相同的$http
函数两次,不同的是param
谢谢。正如@maurycy在他的评论中指出的那样,保持控制器尺寸较小的方法是将常用功能保留在服务中。考虑这项服务:
app.service('Customers',
[ '$http',
function($http) {
return {
byName: byName
};
function byName(name) {
return $http({
url: "http://localhost:8080/cordovaprojects/123m/www/customer.php",
method: "GET",
params: {'name': name}
});
}
}
]
);
然后,您可以在如下控制器中使用此服务:
app.controller('customersCtrl', [
'$scope', 'Customers',
function($scope, Customers) {
$scope.myData = null;
Customers.byName('powercom')
.then(function(response) {
$scope.myData = response;
});
}
]);
<input type="text" data-ng-model="customerName" />
<button data-ng-click="lookupCustomer()">
Lookup Customer
</button>
<div ng-controller="customersController as ctrl"><p ng-click="ctrl.newSearch">Hello</p></div>
它比您现在拥有的要短一些,而且它是分开的,使得它能够用于应用程序的任何其他部分(以及更容易测试)。如果端点发生了变化,那么您只有一个点需要改变,因为它已经在其他任何地方使用过,所以您就完成了
更新
要在ng单击时绑定,我假设您有一个绑定到本地模型的输入,以及一个按钮作为单击目标,如下所示:
app.controller('customersCtrl', [
'$scope', 'Customers',
function($scope, Customers) {
$scope.myData = null;
Customers.byName('powercom')
.then(function(response) {
$scope.myData = response;
});
}
]);
<input type="text" data-ng-model="customerName" />
<button data-ng-click="lookupCustomer()">
Lookup Customer
</button>
<div ng-controller="customersController as ctrl"><p ng-click="ctrl.newSearch">Hello</p></div>
您可以在
lookupCustomer
中添加检查以防止出现边缘情况(无需查找空的客户名称),但这应该适合您。正如@maurycy在其评论中所指出的,保持控制器规模较小的方法是在服务中保留常用功能。考虑这项服务:
app.service('Customers',
[ '$http',
function($http) {
return {
byName: byName
};
function byName(name) {
return $http({
url: "http://localhost:8080/cordovaprojects/123m/www/customer.php",
method: "GET",
params: {'name': name}
});
}
}
]
);
然后,您可以在如下控制器中使用此服务:
app.controller('customersCtrl', [
'$scope', 'Customers',
function($scope, Customers) {
$scope.myData = null;
Customers.byName('powercom')
.then(function(response) {
$scope.myData = response;
});
}
]);
<input type="text" data-ng-model="customerName" />
<button data-ng-click="lookupCustomer()">
Lookup Customer
</button>
<div ng-controller="customersController as ctrl"><p ng-click="ctrl.newSearch">Hello</p></div>
它比您现在拥有的要短一些,而且它是分开的,使得它能够用于应用程序的任何其他部分(以及更容易测试)。如果端点发生了变化,那么您只有一个点需要改变,因为它已经在其他任何地方使用过,所以您就完成了
更新
要在ng单击时绑定,我假设您有一个绑定到本地模型的输入,以及一个按钮作为单击目标,如下所示:
app.controller('customersCtrl', [
'$scope', 'Customers',
function($scope, Customers) {
$scope.myData = null;
Customers.byName('powercom')
.then(function(response) {
$scope.myData = response;
});
}
]);
<input type="text" data-ng-model="customerName" />
<button data-ng-click="lookupCustomer()">
Lookup Customer
</button>
<div ng-controller="customersController as ctrl"><p ng-click="ctrl.newSearch">Hello</p></div>
您可以在
lookupCustomer
中添加检查以防止出现边缘情况(无需查找空的客户名称),但这应该适合您。如果您创建用于获取数据的服务会更好
app.service('dataService', function($http) {
this.get = function get(param) {
return $http({
url: "http://localhost:8080/cordovaprojects/123m/www/customer.php",
method: "GET",
params: {'name': param}
});
}
});
app.controller('customersCtrl', function($scope, dataService) {
dataService.get('powercom').then(function(response) {
$scope.myData = response.data.records
});
$scope.newSearch = function() {
$scope.newSea = $scope.sea;
dataService.get($scope.newSea).then(function(response) {
$scope.myData = response.data.records
});
};
});
并且在$scope中创建函数也不是必需的。您可以使用“this”并通过控制器名称或别名访问控制器中的数据,如下所示:
app.controller('customersCtrl', [
'$scope', 'Customers',
function($scope, Customers) {
$scope.myData = null;
Customers.byName('powercom')
.then(function(response) {
$scope.myData = response;
});
}
]);
<input type="text" data-ng-model="customerName" />
<button data-ng-click="lookupCustomer()">
Lookup Customer
</button>
<div ng-controller="customersController as ctrl"><p ng-click="ctrl.newSearch">Hello</p></div>
您好
如果您创建用于获取数据的服务,效果会更好
app.service('dataService', function($http) {
this.get = function get(param) {
return $http({
url: "http://localhost:8080/cordovaprojects/123m/www/customer.php",
method: "GET",
params: {'name': param}
});
}
});
app.controller('customersCtrl', function($scope, dataService) {
dataService.get('powercom').then(function(response) {
$scope.myData = response.data.records
});
$scope.newSearch = function() {
$scope.newSea = $scope.sea;
dataService.get($scope.newSea).then(function(response) {
$scope.myData = response.data.records
});
};
});
并且在$scope中创建函数也不是必需的。您可以使用“this”并通过控制器名称或别名访问控制器中的数据,如下所示:
app.controller('customersCtrl', [
'$scope', 'Customers',
function($scope, Customers) {
$scope.myData = null;
Customers.byName('powercom')
.then(function(response) {
$scope.myData = response;
});
}
]);
<input type="text" data-ng-model="customerName" />
<button data-ng-click="lookupCustomer()">
Lookup Customer
</button>
<div ng-controller="customersController as ctrl"><p ng-click="ctrl.newSearch">Hello</p></div>
您好
要保持干燥,您可能应该将$http转换为服务或至少一个功能此问题更适合于干燥此问题您可能应该将$http转换为服务或至少一个功能此问题更适合@abbas更新的答案,带有ng click示例此功能在ng click
上正常工作,但默认情况下“powercom”已消失。谢谢,我已编辑您的答案以获取默认值.NP。我从编辑中看出了你的意思。一开始我对你问的问题感到困惑。@abbas仅供参考-编辑你的编辑以使你的编辑更准确DRY@abbas更新了ng click示例的答案这在ng click
上运行良好,但默认情况下“powercom”已消失。谢谢,我已编辑了您的答案以获取默认值。NP。我从编辑中看出了你的意思。一开始我对你的要求感到困惑。@abbas仅供参考-编辑你的编辑以使编辑更加枯燥