1
我正在運行angularJS應用程序。我正在採用顏色選擇器並將所選顏色分配給div.my代碼對於單個記錄工作正常。對於集合(NG-重複),如果我們選擇特定條目的任何一種顏色,然後該顏色分配給所有records.My代碼如下AngularJS手錶列表
TS(打字稿)文件
let mainAngularModule = angular.module("acc-management", ['ngMaterial', 'ngRoute']);
mainAngularModule.config(routeConfig);
routeConfig.$inject = ['$routeProvider'];
function routeConfig($routeProvider, $locationProvider) {
$routeProvider
.when('/it', {
templateUrl: 'linkmanager.html'
})
.when('/UserDefinedElement', {
templateUrl: 'UserDefinedElementType.html',
controller: 'userdefinedelementtype as UDETController'
})
}
class UserTypeRecord {
public Id: number;
public Name: string;
public colorcode: any;
}
class UserDefinedElementTypeController {
private url: string;
private token: string;
public static $inject = ["$scope", '$http', '$q'];
private TestString: any;
public mycolor: string = "#f0f0f0";
public divStyle: any;
public usertypearray: UserTypeRecord[];
constructor(private $scope: ng.IScope) {
this.url = "https://apidev.smartfacts.com/sfit/";
this.watchForColor();
this.usertypearray = [
{ Id: 1, Name: "bhushan" },
{ Id: 2, Name: "suryakant" }
];
}
private watchForColor() {
this.$scope.$watch(() => this.mycolor,
(newVal, oldval) => {
console.log('this.scope', this.$scope);
this.divStyle = {
'background-color': newVal,
'color': 'white',
'width': '100px',
'height': '100px',
'font-size': 'large'
}
});
}
}
HTML代碼
<div class="demo-md-panel-content">
<table>
<thead>
<tr>
<th style="width:15%">Symbol</th>
<th style="width:15%">Name</th>
<th style="width:15%">Color</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in UDETController.usertypearray">
<td style="text-align:center">
<span ng-style="UDETController.divStyle">Testing</span>
</td>
<td>
<input type="text" ng-model="x.Name" />
</td>
<td>
<input type="color" ng-model="UDETController.mycolor" />
</td>
</td>
</tr>
</tbody>
</table>
</div>
當我選擇特定的行/記錄的顏色只是顏色應該被分配或適用於特定的(行)的div它不應該被分配給其他(行)DIV
ü意味着我需要mycolor我UserTypeRecord陣列?截至目前它不在我的陣列,所以我不能把x.mycolor – Bob
我更新了我的答案,希望這將是有益的,有一個美好的一天。 –
在HTML頁面ng-style =「{'background-color':x.Color}」如果我們使用'x',它意味着'usertypearray'的obj,我們可以在ng的幫助下訪問那個數組中的元素-重複。在我的數組中,我沒有將顏色作爲數組的一個元素,您希望我將顏色作爲數組的一部分? – Bob