AngularJS Material使用md-theme-style属性插入多个(大约30个)样式标记.我想这是某种性能调整,但我宁愿自己做 - 我不需要外部框架来以这种讨厌的方式污染我的HTML.关于如何摆脱样式标签的任何想法?
我使用$ q.when来包装其他lib promises.它就像一个魅力,但是当我尝试在Karma中运行它时,即使我运行$ digest甚至超时后,promise仍然会解决(done()永远不会被执行).这是示例代码:
describe('PouchDB', function () {
var $q, $rootScope;
beforeEach(inject(function (_$rootScope_, _$q_) {
$rootScope = _$rootScope_;
$q = _$q_;
}));
it("should run", function (done) {
function getPromise() {
var deferred = Q.defer();
deferred.resolve(1);
return deferred.promise;
}
$q.when(getPromise())
.then(function () {
done(); // this never runs
});
$rootScope.$digest();
});
Run Code Online (Sandbox Code Playgroud)
为什么?这是什么原因?我真的无法得到它.
更新(解决方法)
我不明白为什么$ q.when在Karma中没有得到解决 - 它有nextTick功能,但我无法调试问题.相反,我放弃了$ q.when并编写了简单的函数,将PouchDB(或任何其他类似的Q)转换为$ q:
.factory('$utils', function ($q, $rootScope) {
return {
to$q: function (promise) {
var deferred = $q.defer();
promise.then(function …Run Code Online (Sandbox Code Playgroud) 使用traceur和SystemJS使用ES6这种形式是正确的:
import _ from 'lodash';
Run Code Online (Sandbox Code Playgroud)
对于Typescript它是不够的 - 我得到错误error TS2307: Cannot find module 'lodash'所以,我安装'lodash.d.ts':
/// <reference path="lodash/lodash.d.ts" />
import _ from 'lodash';
Run Code Online (Sandbox Code Playgroud)
现在,我得到:Module '"lodash"' has no default export.来自Typescript编译器
所以,我尝试'节点样式':
/// <reference path="lodash/lodash.d.ts" />
let _ = require('lodash');
Run Code Online (Sandbox Code Playgroud)
我得到:Uncaught (in promise) Error: require is not a function在浏览器中
最后:
import _ = require('lodash');
Run Code Online (Sandbox Code Playgroud)
并且它有效,但它的'旧形式'不适合ES6.
有没有一种正确的方法可以使用ES6样式的Typescript导入非打字稿模块?
(打字稿1.6.2)
我正在使用查询系统,允许用户从动态字段列表构建查询,我想在URL中持久查询.
通常我会这样做:
$stateProvider.state('alerts', {
url: '/alerts/list?p1&p2&p3'
})
Run Code Online (Sandbox Code Playgroud)
在控制器中我可以用$ stateParam读取params:
console.log($stateParams.p1)
Run Code Online (Sandbox Code Playgroud)
我可以在URL中保存查询:
$state.go($state.current, {p1:'1', p2: '2'}, {location: true, inherit:true, notify:false})
Run Code Online (Sandbox Code Playgroud)
但问题是我不能宣布所有的参数.
我想要做:
$state.go($state.current, {p1:'1', p2: '2', p3:'3', p4: '4', p5:'5'}, {location: true, inherit:true, notify:false})
Run Code Online (Sandbox Code Playgroud)
但是ui-router忽略了没有声明状态的params.
我知道$ location.search让我可以访问URL搜索部分,但是如何设置URL(不更改状态和重新加载页面)?
我是Angular的新手,并试图理解高级指令API - 我想使用指令元素属性在编译函数中重新创建指令模板.但是当我没有模板集(或模板是空字符串)而不是访问隔离指令范围时,我访问父(控制器)范围.此外 - 这适用于Angular 1.1但不适用于1.2
这是HTML:
<div class="container" ng-app="app" ng-controller="AppController">
<sandbox title="Attribute Title"></sandbox>
</div>
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
var app = angular.module('app', [], function () {});
app.controller('AppController', function ($scope) {
$scope.title = "AppController title";
});
app.directive('sandbox', function ($log, $compile) {
return {
restrict: 'E',
scope: {
title: "@"
},
controller: function ($scope, $element, $attrs) {
$scope.title = "Directive Controller title";
},
template: '<h1>Template</h1>', // change it to: '' and Run, than change Angular to 1.2.x
compile: function (tElement, tAttrs) {
tElement.append('<h2> Title …Run Code Online (Sandbox Code Playgroud) 我收到以下代码的警告,我不明白为什么.
它类似于这个问题:未处理的拒绝原因(应该是空的)
但......
我很确定我正在处理所有错误,为什么要警告?
这是JSFiddle:http://jsfiddle.net/yoorek/jLLbR/
function run(number) {
var deferred = Q.defer();
if (number % 2) {
deferred.reject(new Error('Error for ' + number));
} else {
deferred.resolve(number);
}
return deferred.promise;
}
var promises = [], data = [1, 2, 3, 4, 5];
data.forEach(function (item) {
var promise;
promise = run(item)
.then(function (result) {
log.info('Success : ' + result);
})
.catch (function (error) {
log.info('Error Handler in loop ' + error.message);
});
promises.push(promise);
});
Q.all(promises)
.then(function () …Run Code Online (Sandbox Code Playgroud) 我需要动态编译 html 并将其作为文本从函数传递。所以,我有这个代码(用于调试目的的简化版本):
angular.module('app', [])
.run(function($rootScope, $compile){
var data = ["1","2"];
var html =
'<div>' +
'<ul>' +
'<li ng-repeat="score in data">{{score}}</li>' +
'</ul>'+
'</div>';
var el = angular.element(html);
$rootScope.data = data;
var result = $compile(el)($rootScope);
console.log(result.html());
})
Run Code Online (Sandbox Code Playgroud)
结果只是:
<ul><!-- ngRepeat: score in data --></ul>
Run Code Online (Sandbox Code Playgroud)
所以,看起来 ngRepeat 不会“重复”“li”元素。
为什么?
JsFiddle: http: //jsfiddle.net/yoorek/K4Cmk/
(我知道 DOM 操作应该在指令等中,并且我知道如何以其他方式进行操作,但我需要理解为什么这不起作用)
我在index.html中有这个SystemJS配置:
<body>
<script src="node_modules/systemjs/dist/system.js"></script>
<script>
System.config({
defaultJSExtensions: true,
transpiler: 'typescript',
map: {
typescript: 'node_modules/typescript/lib/typescript.js'
},
packages: {
"ts": {
"defaultExtension": "ts"
}
},
});
System.import('ts/main');
</script>
</body>
Run Code Online (Sandbox Code Playgroud)
main.ts:
let a = [1, 2, 3];
let b = [1, 2, 3];
Run Code Online (Sandbox Code Playgroud)
我明白了:Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode.它看起来像SystemJS没有编译文件.
当我在第一行添加import语句时,它完美地工作:
import * as ts from 'typescript'; // or any other package
let a = [1, 2, 3];
let b = [1, 2, 3]; …Run Code Online (Sandbox Code Playgroud) angularjs ×5
systemjs ×2
angular ×1
jasmine ×1
javascript ×1
karma-runner ×1
promise ×1
q ×1
typescript ×1