我目前正在探索在AngularJS中处理应用程序范围异常的可能方法.
我们真正想要避免的一件事是在嵌套的try/catch块中包装应用程序的多个部分,但是干净地处理事情 - 即抛出异常以响应promise.
到目前为止进展
一些简短的设计目标:
我团队目前的倾向是编写一个服务来处理异常,这会暴露一系列简单的调用:
exceptionService.warn('exception_token');
exceptionService.crit('another_exception_token');
Run Code Online (Sandbox Code Playgroud)
然后,该服务将格式化"异常"对象并从rootscope广播.这将允许默认处理程序监视任何广播并应用默认操作,并允许在其他范围中设置自定义侦听器,这可以处理更具体的条件 - 即禁用UI的一部分.
var exception = {
token: 'exception_token',
severity': 'crit'
};
// broadcast exception
$rootScope.$broadcast(
'application_exception',
exception
);
Run Code Online (Sandbox Code Playgroud) 我是Angular的新手并试图理解"x-"和"data-"前缀的含义.在指令文档(http://docs.angularjs.org/guide/directive)中,它说这些前缀将使指令"符合HTML验证器".这到底是什么意思?
我正在尝试在自定义指令标记上应用ng-style属性,有点像这样:
<my-directive ng-style="myStyle"></my-directive>
Run Code Online (Sandbox Code Playgroud)
在控制器里面我有:
$scope.myStyle = {
"background": "red"
}
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用.当我检查HTML'MyStyle'时,没有渲染.当我在常规div上应用相同的ng-style标记时,它会正确呈现.
为什么ng-style不适用于自定义指令标签?
我对Angular很新,而现在我只是试图让我的所有路线都按照我的意愿设置和工作.
设置:
当用户导航到某些页面时(/settings对于此示例),应用程序应检查是否有用户已登录.如果有照常继续.否则,用户应该转到登录页面(/login).
我想要的:
用户成功登录后,应该转到他们最初尝试访问的页面(/settings)
我的问题: 是否有"Angular方式"记住用户试图去的地方?
相关代码:
app.js
.when('/settings', {
templateUrl: '/views/auth/settings.html',
controller: 'SettingsCtrl',
resolve: {
currentUser: function($q, $location, Auth) {
var deferred = $q.defer();
var noUser = function() {
//remember where the user was trying to go
$location.path("/login")
};
Auth.checkLogin(function() {
if (Auth.currentUser()) {
deferred.resolve(Auth.currentUser());
} else {
deferred.reject(noUser());
}
});
return deferred.promise;
}
}
})
Run Code Online (Sandbox Code Playgroud)
login.js
$scope.submit = function() {
if(!$scope.logInForm.$invalid) {
Auth.login($scope.login, $scope.password, $scope.remember_me)
//go to the page the user …Run Code Online (Sandbox Code Playgroud) redirect login angularjs angularjs-routing angularjs-authentication
我在AngularJS中设置了两个指令来设置自定义错误消息:
errors - >显示表单的错误消息
错误消息 - >在输入上设置自定义错误消息
出于某种原因,每当我将error-message指令添加到元素时,ng-model绑定不再起作用(但验证确实如此).
见这里http://jsfiddle.net/apohl/A8Vgk/111/
请帮忙 :)
我有ng-click问题(我使用的是角度1.0.4).首先点击工作但第二个没有.
<div class="menu-group" ng-repeat="module in modules">
<div ng-click="toggle($event, $parent)" class="group-head">{{module.group.name}} <span class="{{module.group.icon}}"></span></div>
<ul class="menu collapsed" ng-init="items = module.group.items">
<li ng-repeat="item in items" ng-click="openCategory($event, '{{item.name}}')">{{item.display}}</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
生成的代码看起来很好:
<li ng-repeat="item in items" ng-click="openCategory($event, 'simpleName')" class="ng-scope ng-binding">Simple name</li>
Run Code Online (Sandbox Code Playgroud) 什么是宣布服务的最佳方式我发现这2种不同的方式我似乎无法看出差异:第一种方法:
angular.module('app', [])
.factory('Data', ['$http',function($http){
return {
get: function(fileName,callback){
$http.get(fileName).
success(function(data, status) {
callback(data);
});
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
第二种方法:
angular.module('app', [])
.factory('Data', ['$http', function($http){
var Url = "data.json";
var Data = $http.get(Url).then(function(response){
return response.data;
});
return Data;
}]);
Run Code Online (Sandbox Code Playgroud)
哪一个更好,为什么?提前致谢.
我想从自定义指令输出一些标记,但仅当模型包含一些文本时.
我几乎就在那里,但我不太确定如何在模型改变时打开/关闭模板,如果这甚至是最有效的方式.
这是标记:
<div data-ng-controller="test">
<div class="box">
<input type="text" id="search" maxlength="75" data-ng-model="keywords" />
</div>
<searched data-ng-model="keywords"></searched>
</div>
Run Code Online (Sandbox Code Playgroud)
JS:
var app = angular.module('myApp', []);
app.directive('searched', function(){
return {
restrict: 'E',
replace: true,
scope: {
keywords: '=ngModel'
},
template: '<span><span class="field">You searched for:</span> {{ keywords }}</span> ',
link: function(scope, element, attrs) {
scope.$watch('keywords', function(newVal, oldVal){
if(newVal === null) {
// Output nothing!
} else {
// Output Template as normal.
}
}, true);
}
};
});
app.controller("test", ['$scope', function($scope) {
$scope.keywords = …Run Code Online (Sandbox Code Playgroud)