小编Cos*_*bei的帖子

如何正确将角度2(npm)升级到最新版本?

最近我在https://angular.io/docs/ts/latest/tutorial/上开始了Angular 2教程.

然后我继续使用Angular 2 beta 8.现在我恢复了教程,最新的测试版是beta 14.

如果我只是更新npm更新一些模块(预装了教程)更新但不是Angular2(我可以看到npm ls).

如果我做npm更新角度2npm更新angular2@2.0.0beta.14它也没有做任何事情.

npm angular

124
推荐指数
5
解决办法
16万
查看次数

要求模板的多个指令

我有以下HTML:

<div style="border:1px solid; height:300px; width:500px; position:relative;  left:100px" id="canvas">
  <tbox ng-repeat="tb in textBoxes" ng-model="tb">
  </tbox>
</div>
Run Code Online (Sandbox Code Playgroud)

以及2个指令

appModule.directive('resizable', function($compile, $document) {
  return {
    restrict: "A",
    template: '<div ng-style="{top:tb.x, left:tb.y, height:tb.height, width:tb.width}"  ng-transclude><span class="scale">s</span></div>',
    transclude: true,
    replace: true,
    require: 'ngModel'
  }
});

appModule.directive('tbox', function($document) {
  return {
    restrict: "E",
    template: '<div class="editbox" resizable editoptions>{{tb.text}}</div>',
    replace: true
  }
});
Run Code Online (Sandbox Code Playgroud)

究竟是什么角度投掷我的意思是什么?

Error: Multiple directives [tbox, resizable] asking for template on: <div class="editbox" resizable="" editoptions="" ng-repeat="tb in textBoxes" ng-model="tb">
Run Code Online (Sandbox Code Playgroud)

jsfiddle at http://jsfiddle.net/sEZM3/

javascript angularjs angularjs-directive

36
推荐指数
5
解决办法
4万
查看次数

Angular 2同一页面上的多个组件

我正在使用app.component.ts文件中的Angular 2快速入门代码.

该文件如下所示:

import {Component} from 'angular2/core';

@Component({
    selector: 'app',
    template: `<h1>Title Here</h1>'
})
export class AppComponent { }
Run Code Online (Sandbox Code Playgroud)

这按预期工作.

我想要做的是在同一页面上添加另一个组件...所以我尝试了这个:

import {Component} from 'angular2/core';
import {ComponentTwo} from 'angular2/core';

@Component({
    selector: 'app',
    template: `<h1>Title Here</h1>'
}),

@Component({
    selector: 'appTwo',
    template: `<h1>Another Title Here</h1>'
})
export class AppComponent { }
Run Code Online (Sandbox Code Playgroud)

这不起作用......是我做错了还是不允许这样做?

javascript angular

14
推荐指数
1
解决办法
3万
查看次数

Angular 1.5组件绑定:检查回调是否存在

我有一个简单的contactList组件,它有2个绑定:contactsonRemove.

  • contacts 只是一组要显示的联系人
  • onRemove 是一个回调函数
app
  .component('contactList', {
    template: 
      `<div ng-repeat="c in $ctrl.contacts">
         {{c.name}}
         <div ng-click="$ctrl.onRemove({contact: c})">Remove</div>
       </div>`,
    bindings: {
      contacts: '<',
      onRemove: '&'
    },
    controller: function() {}
  })
Run Code Online (Sandbox Code Playgroud)

我在我的应用程序中多次使用此组件.而onRemove回调可以表现不同,这取决于其中contactList组件习惯.例:

  • contactMainView(=组件)显示搜索栏以及使用该contactList组件生成的联系人列表.onRemove将从数据库中删除联系人.

  • groupMembersView使用contactList组件显示属于给定组的所有联系人.这里不应该删除联系人,但onRemove不会设置.


思路:

首先我想,我可以使用ng-if="$ctrl.onRemove"但不起作用,因为onRemove它永远不会undefinedcontactList组件中.console.log(this.onRemove)总是打印:function(locals) { return parentGet(scope, locals); }

当然我可以使用另一种showRemove: '@'绑定,但这对我来说看起来并不干净.


你有任何想法或更好的解决方案来完成任务吗?

angularjs angularjs-directive angularjs-bindings angularjs-components

12
推荐指数
1
解决办法
2949
查看次数

如何使用角度2中的ngFor将数组循环到一些对象

我循环遍历一个有6个对象的数组,并使用ngFor,我想只循环4个元素.我怎么能这样做?

<div class="item active" *ngFor="#data of lengthArray">
 content 
</div>
Run Code Online (Sandbox Code Playgroud)

在LengthArray我有6但是如何循环到4个记录?

而且我想从另一个div中的第4条记录循环到第6条记录.我怎样才能从第4条记录开始?

html ngfor angular

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

Angular ng - 不需要使用自定义指令

我使用Angularjs 1.5版来验证表单中的输入.

  • ng-required用于验证所需的所有输入

但是,它不能使用呈现组合的自定义指令.组合根据传递给它的参数检索项目名为'listId'.然后,它使用ng-repeat在'lookupItems'上迭代.我想有些东西丢失了,比如ngModel.为什么以及如何实施它?

组合指令:

app.directive('combo', function($http) {
    return {
        restrict: 'AE',
        template: '<div class="input-group"> <select ng-model="selectedItem">' +
            '<option  ng-repeat="option in lookupItems" value={{option.ListValueID}}>{{option.Translation.Value}}</option></select>' +
            '  {{selectedItem}} </div>',
        replace: true,
        scope: {
            listId: '=',
            defaultItem: '=',
            selectedItem: '='
        },
        controller: function($scope) {
            $http({
                method: 'GET',
                url: '/home/listvalues?listid=' + $scope.listId
            }).then(function(response) {
                $scope.lookupItems = response.data;
            }, function(error) {
                alert(error.data);
            });
        },
        link: function(scope, element, attrs) {}
    };
});
Run Code Online (Sandbox Code Playgroud)

html视图:迭代包含要呈现的控件类型的属性,然后根据'attribute.Required'将其设置为布尔值,这是真的.

<form name="profileAttributesForm" ng-controller="metadataCtrl" class="my-form">
    <div ng-repeat="a in attributes">
        <div ng-if="a.DataType == 1"> …
Run Code Online (Sandbox Code Playgroud)

required-field angularjs angularjs-directive angularjs-forms

4
推荐指数
1
解决办法
6354
查看次数

Angular 2.0,Typescript和'FORM_DIRECTIVES'模块

我正在使用Angular 2.0 Alfa 35.当我导入时FORM_DIRECTIVES(这是formDirectivesAlfa 35 的新名称)我收到了一个Typescript错误:

error TS2305: Module '"angular2/angular2"' has no exported member 'FORM_DIRECTIVES'.

这是ts定义中的错误吗?我可以修复/覆盖它吗?

javascript typescript angular

3
推荐指数
1
解决办法
7804
查看次数

AngularJS - ng - 如果数组中有特定值

我希望ngIf在范围内的数组中有特定值时触发.

所以我在我的范围内有这个:

var orders = [{
    "order_no": 1,
    "color": ["blue", "pink", "red"]
}, {
    "order_no": 2,
    "color": ["yellow", "blue", "white"]
}];
Run Code Online (Sandbox Code Playgroud)

我希望有一个ngIf显示一个元素,例如,如果其中一个颜色数组中存在黄色(假设我事先不知道数据范围,但事先知道结构).

我发现这样做的唯一方法是事先知道范围的结构,所以fx ng-if="orders[1].color[0] === 'yellow'"

谢谢!

angularjs angular-ng-if

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