我使用MaterializeCSS 0.9.8(與角兌現指令)和角度1.5.0MaterializeCSS添加自動完成場動態地
我的目標是在一個表中生成一些行,每行那些需要有一個自動完成基本一致自動完成數據(用戶列表),但我不能讓這些工作....
這裏是我走到這一步,我已經做了一個小例子,顯示我的問題的代碼。
HTML
<!DOCTYPE html>
<html lang="en" ng-app="myApp" class="no-js">
<head>
<!--Import Google Icon Font-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!--Import materialize.css-->
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/css/materialize.min.css" media="screen,projection" />
<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/js/materialize.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-materialize/0.2.2/angular-materialize.min.js"></script>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
<div ng-controller="myController" ng-init="getUsers()">
<table>
<thead>
<tr>
<th>User</th>
<th>login</th>
</tr>
</thead>
<tbody>
<tr style ="border : 1px solid silver" ng-repeat="a in accesses">
<td>
<input type="text" placeholder="User" class="autocompleteUser" ng-model='a.fullName'>
</td>
<td>{{a.login}}</td>
</tr>
</tbody>
</table>
<a id="addButton" ng-click="addAccess()" href="#">Add user</a>
{{accesses}}
</div>
</body>
</html>
test.js
'use strict';
$(document).ready(function() {
$('select').material_select();
});
// Declare app level module which depends on views, and components
var module = angular.module('myApp', ['ui.materialize'])
.controller('myController', ['$scope', function($scope) {
$scope.users = [
{login : 'jdoe', fullName : 'John Doe'},
{login : 'fbar', fullName : 'Foo Bar'},
{login : 'mmax', fullName : 'Mad Max'}
];
$scope.accesses = [];
/**
* Add a new access to the table
*/
$scope.addAccess = function() {
$scope.accesses.push({
login : '',
fullName : ''
});
};
/**
* Get all users in the DB
*/
$scope.getUsers = function() {
//Simulate HTTP request
$scope.usersAutocomplete = {
'John Doe' : null,
'Foo Bar' : null,
'Mad Max' : null
};
$('input.autocomplete').autocomplete({
data: $scope.usersAutocomplete,
limit: 20,
onAutocomplete: function(val) {
$scope.getUserByName(val);
},
minLength: 1
});
};
/**
* Get a user given his name and surname
* @param fullName name + surname
*/
$scope.getUserByName = function (fullName) {
var row = $scope.users.filter(function (user) {
return user.fullName === fullName;
});
//Assign row.login here
};
}]);
如果我初始化場部分自動完成,工程....但如果我動態地宣佈這些領域(就像我在我的表格中添加一行時那樣)。
有沒有辦法讓這個動態地或至少一種變通方法工作呢?
感謝
編輯
我試圖插入線Materialize.updateTextFields(),但仍然沒有工作...
感謝您的回答,它工作得很好!我離解決方案並不遙遠,但我從來沒有料到CSS會出現問題... –
@Alexi Coard歡迎:)。始終嘗試通過更新庫來解決問題。我得到賞金嗎? ;) – Shantanu
當然,你會得到這個賞金;)。我將下載Materialise的工作版本並從文件系統提供。將在未來更容易 –