我不知道什么是最佳做法以及我应该使用什么.
以下两种方法有什么区别?
module.service(..);
Run Code Online (Sandbox Code Playgroud)
和
module.factory(..);
Run Code Online (Sandbox Code Playgroud) 我熟悉以下方法来实现控制器之间的通信.
还有其他人吗?有更好的方法/最佳实践吗?
$broadcast/$emit.controller("Parent", function($scope){
$scope.$broadcast("SomethingHappened", {..});
$scope.$on("SomethingElseHappened", function(e, d){..});
})
.controller("Child", functions($scope){
$scope.$broadcast("SomethingElseHappened", {..});
$scope.$on("SomethingHappened", function(e, d){..});
})
.controller("AnotherChild", functions($scope){
$scope.$on("SomethingHappened", function(e, d){..});
});
Run Code Online (Sandbox Code Playgroud)
或者,从视图:
<button ng-click="$broadcast('SomethingHappened', data)">Do Something</button>
Run Code Online (Sandbox Code Playgroud)
好处:
缺点:
$rootScope,使用<div ng-controller="Parent">
<div ng-controller="Child">
<div ng-controller="ChildOfChild">
<button ng-click="someParentFunctionInScope()">Do</button>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
或者,在代码中
.controller("ChildOfChild", function($scope){
$scope.someParentFunctionInScope();
});
Run Code Online (Sandbox Code Playgroud)
好处:
缺点:
$rootScope,使用$watch控制器仅响应范围暴露数据的变化,从不调用函数.
.controller("Parent", function($scope){
$scope.VM = {a: "a", b: "b"};
$scope.$watch("VM.a", function(newVal, oldVal){
// react
}); …Run Code Online (Sandbox Code Playgroud) 我有多个路由器的多个控制器:
app.controller('FirstController', function ($scope) {
$scope.func = function () {
console.log('route 1');
}
}
app.controller('SecondController', function ($scope) {
$scope.func = function () {
console.log('route 2');
}
}
...
Run Code Online (Sandbox Code Playgroud)
以及使用$scope.func这种方式的指令:
app.directive('thedirective', function () {
return {
link: function (scope, $element, attrs) {
$scope.func(attrs.thedirective);
}
}
});
Run Code Online (Sandbox Code Playgroud)
$scope.func每个控制器都有所不同.我希望$ scope.func在route1中记录"route 1",FirstController是当前控制器,在路由2中记录"route 2",但只有"rout 1"是我在控制台得到的.请你告诉我为什么改变路线不会改变$指令的范围?
我真的是 AngularJS 的新手,所以这可能真的很明显!
我有一个服务,它会做一些基本的检查,然后返回信息。我在控制器和函数中调用此服务。
当我在控制器中调用它时,它工作正常,没有错误。
当我在函数中调用它时,我得到
angular.js:9101 ReferenceError: systemService 未定义
在有效的控制器中调用服务:
myApp.controller('continueController', ["$scope", "$rootScope", 'systemService', function ($scope, $rootScope, systemService) {
$scope.ContinueAngularMethod = function () {
$rootScope.field = 'payment';
$scope.field = $rootScope.field;
$scope.notApp = '1';
console.log("I don't error in here");
$scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
$rootScope.$apply();
}
}]);
Run Code Online (Sandbox Code Playgroud)
在不起作用的函数中调用服务:
function MyCtrl($scope) {
$scope.changeme = function () {
console.log("Drop down changes ideally do something here....");
//Call to service
$scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
console.log($scope.ss.field);
console.log($scope.ss.notAppJ);
}
Run Code Online (Sandbox Code Playgroud)
这是我的完整代码:
<script type='text/javascript'>
//Angular stuff
var myApp = …Run Code Online (Sandbox Code Playgroud)