小编all*_*kim的帖子

angular2从指令访问ngModel变量

我正在尝试构建如下所示的日期时间选择器指令.
<input [(ngModel)]="date1" datetime-picker date-only />

并被date1指定为日期,例如,new Date()

当我在html中显示它时,input元素中的文本如下所示
Thu Jan 01 2015 00:00:00 GMT-0500

我希望显示如下所示
2015-01-01 00:00:00

我想使用DatePipe格式化日期WITHIN,而不是显示默认的toString()函数的结果.

我的问题是; "在一个指令中,我如何访问ngModel变量?",例如date1,这样我就可以添加toString()方法.

如果我的方法不对,请告诉我.

directive angular2-ngmodel angular

11
推荐指数
2
解决办法
1万
查看次数

使用mocha,有没有办法存根许多参数?

我们假设我有这门课

class Foo
  def bar(param1=nil, param2=nil, param3=nil)
     :bar1 if param1
     :bar2 if param2
     :bar3 if param3
  end
end
Run Code Online (Sandbox Code Playgroud)

我可以使用以下方法存根整条方法:

Foo.any_instance.expects(:bar).at_least_once.returns(false)
Run Code Online (Sandbox Code Playgroud)

但是如果我只想在bar方法的param1为true时存根,我找不到办法:

我也查看了with和has_entry,它似乎只适用于单个参数.

我期待这样的功能.

Foo.any_instance.expects(:bar).with('true',:any,:any).returns(:baz1)
Foo.any_instance.expects(:bar).with(any,'some',:any).returns(:baz2)
Run Code Online (Sandbox Code Playgroud)

谢谢

.................................................. .编辑以下.............................................

谢谢,纳什

不熟悉rspec,所以我尝试使用any_instance进行单元测试,看起来很有用

require 'test/unit'
require 'mocha'

class FooTest < Test::Unit::TestCase 

  def test_bar_stub 
    foo = Foo.new
    p foo.bar(1)

    Foo.any_instance.stubs(:bar).with { |*args| args[0]=='hee' }.returns('hee')
    Foo.any_instance.stubs(:bar).with { |*args| args[1]=='haa' }.returns('haa')
    Foo.any_instance.stubs(:bar).with { |*args| args[2]!=nil   }.returns('heehaa')

    foo = Foo.new
    p foo.bar('hee')
    p foo.bar('sth', 'haa')
    p foo.bar('sth', 'haa', 'sth')
  end

end
Run Code Online (Sandbox Code Playgroud)

ruby testing ruby-on-rails mocha.js

10
推荐指数
1
解决办法
6425
查看次数

模块中@和@@有什么区别?

假设包含模块而不是扩展模块,模块实例变量和类变量之间有什么区别?

我认为两者之间没有任何区别.

module M
  @foo = 1
  def self.foo
    @foo
  end
end
p M.foo

module M
  @@foo = 1
  def self.foo
    @@foo
  end
end
p M.foo
Run Code Online (Sandbox Code Playgroud)

我一直在模块中使用@ as @@,我最近看到其他代码在模块中使用@@.然后我以为我可能错误地使用它.

由于我们无法实例化模块,因此模块的@和@@之间必须没有区别.我错了吗?

-----------------------添加以下内容--------------------

为了回答有关评论和帖子的一些问题,我还测试了以下内容.

module M
  @foo = 1
  def self.bar
    :bar
  end

  def baz
    :baz
  end
end

class C
  include M
end

p [:M_instance_variabies, M.instance_variables] # [@foo]
p [:M_bar, M.bar] # :bar

c = C.new
p c.instance_variables
p [:c_instance_variabies, c.instance_variables] # []
p [:c_baz, c.baz] :baz
p [:c_bar, c.bar] # undefined …
Run Code Online (Sandbox Code Playgroud)

ruby

8
推荐指数
1
解决办法
869
查看次数

使用Angular UI路由器在AngularJS中具有历史记录的一个页面上的多个视图

在我的AngularJS应用程序中,我有一个包含几个子部分的页面.所有这些子部分都在同一页面上,并且是同一模板的一部分.但是,我想通过它自己的URL访问每个部分,如果匹配,它将向下滚动到正确的部分.

我已经将状态设置为:

.state('about', {
    url: "/about",
    templateUrl: "partials/about.html",
})
.state('about.team', {
    url: "/about/team",
    templateUrl: "partials/about.html"
})
.state('about.office', {
    url: "/about/office",
    templateUrl: "partials/about.html"
})
.state('about.other', {
    url: "/about/other",
    templateUrl: "partials/about.html"
});
Run Code Online (Sandbox Code Playgroud)

在关于页面我有一个菜单,如:

<div class="menu">
    <a ui-sref-active="selected"
    ui-sref="about.team">The Team</a>
    <a ui-sref-active="selected"
    ui-sref="about.office">The Office</a>
    <a ui-sref-active="selected"
    ui-sref="about.other">Other</a>
</div>
Run Code Online (Sandbox Code Playgroud)

此菜单固定在底部,当用户单击链接或通过地址栏直接访问URL时,它应滚动到该部分(更新需要匹配的URL).

但是我不知道该怎么做(通过Angular).总结一下:

  1. 如何通过URL滚动到同一页面上的不同部分?因为我不能使用标准哈希技巧,因为它已经使用哈希作为地址(我们支持IE8).在HTML5模式下我根本不想使用哈希值!它应该在应用程序加载后滚动到页面加载部分,因此需要一些方法来触发在其他内容之后滚动用户的代码...
  2. 如何点击按钮才能完成这项工作?当前单击按钮时,它们会更改URL,但不会滚动到内容.

该页面的HTML看起来像:http://pastebin.com/Q60FWtL7

你可以在这里看到这个应用程序:http://dev.driz.co.uk/AngularPage/app/#/about

有关类似内容的示例(https://devart.withgoogle.com/#/about/your-opportunity),您可以看到它根据网页加载后的网址向下滚动到正确的部分...并且不会不要使用哈希,而是使用ACTUAL网址.

javascript angularjs angular-ui-router

8
推荐指数
1
解决办法
1849
查看次数

angular2指令`无法使用输出元数据读取未定义的属性'subscribe'

关于Angular2指令,我想使用outputs而不是使用@Output因为我有很多自定义事件并且想要保持DRY.

但是,我有TypeError: Cannot read property 'subscribe' of undefined,而且我不知道为什么会这样.

http://plnkr.co/edit/SFL9fo?p=preview

import { Directive } from "@angular/core";

@Directive({
  selector: '[my-directive]',
  outputs: ['myEvent']
}) 
export class MyDirective {
  constructor() {
    console.log('>>>>>>>>> this.myEvent', this.myEvent);
  }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这是使用此指令的app组件

在此输入图像描述

angular2-directives angular

7
推荐指数
1
解决办法
5045
查看次数

Rails 3,仅查看日期格式?

我需要以本地化格式显示日期.即12/31/2013

因此,我在config/initializers/datetime_formats.rb中将默认日期格式设置为该格式

Date::DATE_FORMATS[:default]="%m/%d/%Y"
Time::DATE_FORMATS[:default]="%m/%d/%Y %H:%M"
Run Code Online (Sandbox Code Playgroud)

但是,我的测试在单元测试中失败了,因为有些搜索是基于日期格式的.例如

>> User.find_by_created_at("#{DateTime.now}")
User Load (2.7ms)  SELECT `users`.* FROM `users` 
WHERE `users`.`created_at` = '02/04/2013 14:43' LIMIT 1
Run Code Online (Sandbox Code Playgroud)

当然,我可以更改所有模型以使用所有日期或日期时间搜索来使用Date类或DateTime类而不是字符串.但是,如果我们可以应用仅查看日期格式或仅查看时间格式,我很好奇.

有没有办法应用仅查看日期(或时间)格式?

-----编辑-----

对于具有异常处理的视图,我已经有自定义方法l_or_none.

def l_or_none(object)
  l(object) rescue ''
end  
Run Code Online (Sandbox Code Playgroud)

我只是不想在整个视图中重复这个方法,并寻找一种方法,"如果在视图中调用Date#to_s,以这种方式格式化",而不使用我自己的方法.

为什么我们没有这样的概念?

"如果在视图下使用此对象,则以这种方式覆盖方法"

ruby-on-rails-3

6
推荐指数
1
解决办法
3370
查看次数

Shadow DOM全局​​css继承可能吗?

有没有办法继承:主机元素css样式到shadow DOM?原因是如果我们开始开发Web组件,每个Web组件样式必须在页面上保持一致.

该页面可以具有全局css,并且此全局css样式可以继承到shadow DOM.有::shadow/deep/,但现在已经过时了.

或者,这是否违背模式?如果是这样,为什么?

我发现了这个Q/A,但对我来说似乎已经过时了. Shadow DOM元素可以继承CSS吗?

http://plnkr.co/edit/qNSlM0?p=preview

const el = document.querySelector('my-element');
el.attachShadow({mode: 'open'}).innerHTML = `
  <!-- SEE THIS 'red' is not red -->
  <p class="red">This is defined in Shadow DOM. I want this red with class="red"</p>
  <slot></slot>
`;
Run Code Online (Sandbox Code Playgroud)
  .red {
    padding: 10px;
    background: red;
    font-size: 25px;
    text-transform: uppercase;
    color: white;
  }
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>

<head>
  <!-- web components polyfills -->
  <script src="//unpkg.com/@webcomponents/custom-elements"></script>
  <script src="//unpkg.com/@webcomponents/webcomponentsjs"></script>
  <script src="//unpkg.com/@webcomponents/shadydom"></script>
  <script src="//unpkg.com/@webcomponents/shadycss@1.0.6/apply-shim.min.js"></script>
</head>

<body>

<div>
  <p class="red">I'm outside …
Run Code Online (Sandbox Code Playgroud)

html shadow-dom

6
推荐指数
1
解决办法
871
查看次数

angular.js如何在每次测试中模拟(或存根)xhr请求

在角度js单元测试中,我想为每个测试设置xhr响应(在"it"方法中),而不是在beforeEach方法中,但它似乎不起作用.

这有效

describe('ExampleListCtrl', function(){
  var $scope, $httpBackend;

  beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
    $httpBackend = _$httpBackend_;
    $httpBackend.expectGET('examples').respond([]);   // <--- THIS ONE
    $controller(ExampleListCtrl, {$scope: $scope = $rootScope.$new()});
  }));

  it('should create "examples"  with 0 example fetched', function() {
    expect($scope.examples).toBeUndefined();
    $httpBackend.flush();
    expect($scope.examples).toEqual([]);
  });

});
Run Code Online (Sandbox Code Playgroud)

结果

Executed 8 of 8 SUCCESS (0.366 secs / 0.043 secs)
Run Code Online (Sandbox Code Playgroud)

但是当我将expectGet方法移动到每个方法时,这会失败并出现错误.我不知道为什么.

describe('ExampleListCtrl', function(){
  var $scope, $httpBackend;

  beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
    $httpBackend = _$httpBackend_;
    $controller(ExampleListCtrl, {$scope: $scope = $rootScope.$new()});
  }));

  it('should create "examples"  with 0 example fetched', function() {
    $httpBackend.expectGET('examples').respond([]);  // …
Run Code Online (Sandbox Code Playgroud)

angularjs

5
推荐指数
1
解决办法
2796
查看次数

没有jQuery的AngularJs中的Highcharts?

对于不需要完整jQuery的highcharts,是否有任何指令?这些似乎是目前最受欢迎的,但如果没有jQuery,我无法让它工作:https://github.com/pablojim/highcharts-ng

是否可以编写一个highcharts指令,并且只使用angular包含的jQuery的简易版本?

如果我必须包含完整的jQuery,它会显着影响我的应用程序的加载时间/性能吗?

jquery highcharts angularjs

5
推荐指数
1
解决办法
2574
查看次数

ng-map:设置缩放级别时无法读取undefined属性'apply'

我在angularjs应用程序中使用ngMap.当我尝试在地图元素上设置缩放时,我遇到了错误.以下是Chrome控制台的错误消息:

 TypeError: Cannot read property 'apply' of undefined
    at Wk.eventFunc (ng-map.js:205)
    at Object.T.trigger (main.js:18)
    at kf (main.js:22)
    at Wk.N.set (main.js:21)
    at Wk.setZoom (main.js:29)
    at ng-map.js:1568
    at angular.js:6837
    at forEach (angular.js:323)
    at Object.$get.Attributes.$set (angular.js:6835)
    at interpolateFnWatchAction (angular.js:8157)
Run Code Online (Sandbox Code Playgroud)

当我的map元素的zoom属性触发时:

<map center="{{initCenter.lat}}, {{initCenter.lng}}" zoom="{{zoom}}" on-dragend="dragend()" on-zoom_changed="zoomchanged()">
Run Code Online (Sandbox Code Playgroud)

被绑定到我的角度控制器:

app.controller("mapCtrl", function ($scope, $location, dataContext) {
    $scope.markers = [];

    $scope.initCenter = dataContext.center();
    $scope.zoom = dataContext.zoom();
Run Code Online (Sandbox Code Playgroud)

dataContext是一种在应用程序中公开各种状态变量的服务.我也尝试通过函数设置$ scope.zoom,产生相同的错误.

错误发生在ng-map.js文件的第1568行:

    var observeAndSet = function(attrs, attrName, object) {
  attrs.$observe(attrName, function(val) {
    if (val) {
      void 0;
      var setMethod = …
Run Code Online (Sandbox Code Playgroud)

google-maps angularjs ng-map angularjs-google-maps

5
推荐指数
1
解决办法
2925
查看次数