我正在使用angular and bootstrap ui并试图让bootstrap轮播工作。目前我只是使用文档中提供的演示代码。如果我在页面加载后立即初始化该函数,它就可以正常工作。
然而,我的最终目标是传递一个变量,直到用户输入一些信息后才会定义,所以我需要稍后初始化轮播。当我尝试这样做时,我在控制台中没有出现任何错误,当我检查html时,它显示已经将旋转木马插入到DOM中。然而,它的任何部分都没有出现。我只是得到一个空白页面。 任何帮助将不胜感激。谢谢!
HTML:
$scope.carousel = function(totalMatches){
console.log("called");
console.log(totalMatches);
$scope.myInterval = -1;
var slides = $scope.slides = [];
$scope.addSlide = function() {
var newWidth = 600 + slides.length + 1;
slides.push({
image: 'http://placekitten.com/' + newWidth + '/300',
text: ['More','Extra','Lots of','Surplus'][slides.length % 4] + ' ' +
['Cats', 'Kittys', 'Felines', 'Cutes'][slides.length % 4]
});
};
for (var i=0; i<4; i++) {
$scope.addSlide();
}
};
$scope.carousel(totalMatches);
JS(工作):
$scope.findSomething = function(){
do some stuff in here to define totalMatches
$scope.carousel(totalMatches);
};
$scope.carousel = function(totalMatches){
console.log("called");
console.log(totalMatches);
$scope.myInterval = -1;
var slides = $scope.slides = [];
$scope.addSlide = function() {
var newWidth = 600 + slides.length + 1;
slides.push({
image: 'http://placekitten.com/' + newWidth + '/300',
text: ['More','Extra','Lots of','Surplus'][slides.length % 4] + ' ' +
['Cats', 'Kittys', 'Felines', 'Cutes'][slides.length % 4]
});
};
for (var i=0; i<4; i++) {
$scope.addSlide();
}
};
JS(不工作):
public class MainActivity extends Activity {
private ArrayList<String> data = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView);
generateList();
listView.setAdapter(new MyListAdapter(this, R.layout.item_view, data));
}
private void generateList() {
for (int i = 0; i < 5; i++) {
data.add("row/item " + i);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class MyListAdapter extends ArrayAdapter<String> {
private int layout;
public MyListAdapter(Context context, int resource, List<String>
objects) {
super(context, resource, objects);
layout = resource;
}
@Override
public View getView(final int position, View convertView, ViewGroup
parent) {
ViewHolder minViewHolder = null;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.title = (TextView)
convertView.findViewById(R.id.list_item_text);
viewHolder.button = (Button)
convertView.findViewById(R.id.favorites_button);
viewHolder.button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "sent item " + position + "
to favorites tab", Toast.LENGTH_SHORT).show();
// data.remove(position);
MyListAdapter.this.notifyDataSetChanged();
}
});
convertView.setTag(viewHolder);
} else {
minViewHolder = (ViewHolder) convertView.getTag();
minViewHolder.title.setText(getItem(position));
}
return convertView;
}
}
public class ViewHolder{
TextView title;
Button button;
}
}